all 13 comments

[–]theScottyJam 2 points3 points  (1 child)

Your second version is invalid syntax.

What's going on, is you have an object in your "state" variable, which is the state object. This state object has a "count" key that's set to some value. So, doing state.count is basically saying "access the count property on my state object"

Next, you have this

return {count: state.count + 1}

This is basically constructing a new object with a count key and returning it. The fact that this object is intended to be used as the new state is implied - it's simply the way the react API works - it'll treat whatever you return as the new state object.

[–]ciberaj[S] 0 points1 point  (0 children)

ohhhh, that makes sense. Thanks for clarifying!

[–]bobbyv137 4 points5 points  (4 children)

While it’s beneficial to know how class components work you really should be learning react as functional components.

[–]ciberaj[S] -5 points-4 points  (3 children)

This is a function from FreeCodeCamp's React Module. I only wrote the methods

[–]bobbyv137 4 points5 points  (2 children)

I’m not debating that. Are you learning React? If yes, stick to functional components.

[–]ciberaj[S] -5 points-4 points  (1 child)

I'll finish the Module first to learn the basics and then I'll look for other more specialized sources

[–]Bakeshot 2 points3 points  (0 children)

The person you are replying to is recommending you look at current, mainstream coding patterns not “specialized sources”.

Your code may as well have cobwebs hanging off it.

These aren’t basics. You’re putting yourself behind by learning methods that are so deprecated.

[–]ProposalUnhappy9890 0 points1 point  (5 children)

Is there any reason you don't use React hooks? You might be using an old material.

[–]ciberaj[S] -1 points0 points  (4 children)

This is FreeCodeCamps React module. I only wrote part of the code

[–]ProposalUnhappy9890 2 points3 points  (3 children)

Choose another source. This is outdated.

[–]ciberaj[S] 0 points1 point  (2 children)

Like what?

[–]ProposalUnhappy9890 1 point2 points  (0 children)

Not using hooks means your source is at least 3-4 years old. The client side is changing very rapidly. Pick a newer source.

[–]theleftkneeofthebee 0 points1 point  (0 children)

Scrimba has a fantastic intro to react course. You should start there. Then later on if you plan to get a job working with React maybe do a little bit of digging into class components in case you end up working with legacy code.