It has been four days since I started digging into React.

Image for post

What can I say? It’s a truly powerful tool. Powerful — and not so easy at all. You’ll more than definitely struggle with syntax and new concepts, especially if you’re a newbie. One of these weird but intriguing concepts is a .setState function.

However, don’t get ahead of ourselves! First things first: what is this state thing?

**State **is a plain JavaScript object of React components (which are reusable pieces of code that make our user interface (UI)). That means, in state we can store the values that belong to a component.

But wait! What about props? It is also a JavaScript Object used by React and storing the values. Without diving into the deep dark depth, consider this: props are immutable. That’s the main reason why we need state. You can’t change props, they’re pretty static. Sometimes it works for us, but sometimes we need to make our app more dynamic. And here comes state!

**Important: **you don’t have to use state in every React component! Use it only when you need a parameter that can be altered while running the app.

Remembering this, let’s have a look at the example of how we actually mutate the value with this mysterious .setState.

.setState is a built-in React function. We absolutely need it because the direct assignment of a new value won’t work with state: we should follow the convention. Here is my example inspired by one of my favorite TV show — Doctor Who.

Image for post

example of using .setState

#javascript #react #newbie #flatiron-school #setstate

The mystery of .setState
1.90 GEEK