The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Redux: React Counter Example

Redux: React Counter Example

2:18
Before you use the React Redux bindings, learn how to create a complete simple application with just React and Redux.
Watch this lesson now
Avatar
egghead.io

Before you use the React Redux bindings, learn how to create a complete simple application with just React and Redux.

Avatar
Steve

I can't find any description/explanation of this variant of arrow func syntax:
({
value,
onIncrement,
onDecrement
}) =>

What does putting braces around the arguments signify?

Avatar
Joel

That will "destructure" the argument. Instead of receiving the full object this function will be passed its properties of the respective names.

It's a super useful es6 feature. See more here: https://egghead.io/lessons/ecmascript-6-destructuring-assignment

In reply to Steve
Avatar
Dana

Is creating const's the preferred way to declare components as opposed to creating classes that extend React.Component? Or was this shorthand approach written for the purposes of the tutorial?

Avatar
Dan Abramov

Let’s first clarify that const is not the point here. It only enforces that this binding is not reassigned later, but we could’ve used let (or ES5 var) just as fine when declaring a functional component. In fact

const Counter = ({ value}) => <div>{value}</div>;

is pretty much the same as

function Counter({ value }) {
  return <div>{value}</div>;
}

Now, when should you use functional components instead of classes? I’d say every time it’s possible. Support for them was added in React 0.14 so most examples in the wild don’t use them yet, but it’s usually a better pattern. Functional components match React’s conceptual model more closely. Use classes when you need this.state or lifecycle hooks like componentDidMount, but in most cases, use functional components for simplicity.

In reply to Dana
Avatar
Steven

Hi :)
Thanks for this course, it's very helpful !
I have one qestion, not about Redux... but more by curiosity !

Can you explain why in JSBin this kind of synthax can be written :
=> (

{value}
+
-

);

There are no ' or " or ` ... at the first glance i thought "it's a great new feature of ES6 ... no need to put any symbols for templating..."
But i tried, and it's crash.. of course...

Thanks for your time !

Avatar
Jesse

I would like to see an answer for this as well. Steven did you ever get it it working outside of pastebin?

In reply to Steven
Avatar
Lars-m

Hi Steven
It is perfectly legal ES6 + React.
The counter method receives the props and, using ES6 Object Destructuring Assignment, (see video refered by joel) fetches the three properties of interest.

This rewrite of the Counter method might make it a bit clearer?

const Counter = (props) => { 
  let {value,onIncrement, onDecrement} = props;
  return (
  <div>
    <h1>{value}</h1>
    <button onClick={onIncrement}>+</button>
    <button onClick={onDecrement}>-</button>
  </div>
  )
}```
In reply to Steven
Avatar
Guille

Hi, I am not a React developer, but a quick search I found this. Hope it helps you https://facebook.github.io/react/docs/react-without-jsx.html

In reply to Steven
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?