# Understanding State

**URL:** <https://forum.freecodecamp.org/t/understanding-state/220524>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [August 23, 2018, 4:18pm UTC](https://forum.freecodecamp.org/t/understanding-state/220524 "2018-08-23T16:18:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [August 23, 2018, 4:18pm UTC](https://forum.freecodecamp.org/t/understanding-state/220524/1 "2018-08-23T16:18:50Z")

</div>

So im still trying to wrap my head around react…well state really. Like when to know a component needs state. The state is in one component, and passes it off as a prop to other components if needed. How do I know when to pass it off? Like lets say I have state on a grocery list, and the user wants to edit that list. The list component will have the state say

```auto
this.state = {
carrots: 10,
soup:2,
milk: 2,
bread:2
}

```

Well the user want to change carrots, because who wants 10 things of carrots? Now say there is a button that is pushed that brings up a modal change the carrot state. Would that be another component with the carrot state passed in? Or would that still be handled on the list component?

---

<div class="post-metadata">

**Author:** ![CandiW](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/candiw/32/95324_2.png) [@CandiW](https://forum.freecodecamp.org/u/CandiW)\
**Post date:** [August 23, 2018, 4:36pm UTC](https://forum.freecodecamp.org/t/understanding-state/220524/2 "2018-08-23T16:36:53Z")

</div>

Well, if that modal has an input field where you can change the carrots quantity, then you’d have to have a way to pass the quantity back to the list component. You typically use `props` to help pass data back and forth between components.

I found this article here that was really good: [https://codeburst.io/react-js-pass-data-from-components-8965d7892ca2](https://codeburst.io/react-js-pass-data-from-components-8965d7892ca2)

Hopefully this answers some of your questions better than I could 🙂

---

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [August 23, 2018, 4:45pm UTC](https://forum.freecodecamp.org/t/understanding-state/220524/3 "2018-08-23T16:45:18Z")

</div>

I will give it a look for sure! I’m not in the setting to really sit down and read right now. I will have to look at the react lessons for like the 5th time as well lol. So that moral is obviously going to have onclick when the user hits save or whatever. So I would that onclick function back to the list component?

---

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [August 23, 2018, 5:18pm UTC](https://forum.freecodecamp.org/t/understanding-state/220524/4 "2018-08-23T17:18:52Z")

</div>

Or looking at some of the lessons. I could create a handle change method in the list component, and pass that to the lets call it EditList component? Then when the button is pushed it calls the passed in function which updates the state?

---

<div class="post-metadata">

**Author:** ![CandiW](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/candiw/32/95324_2.png) [@CandiW](https://forum.freecodecamp.org/u/CandiW)\
**Post date:** [September 11, 2018, 4:20am UTC](https://forum.freecodecamp.org/t/understanding-state/220524/5 "2018-09-11T04:20:16Z")

</div>

I’m sorry I’m just now seeing your replies…it’s been quite a busy month for me (I haven’t been on much lately)!

I think it really depends on how you want to approach it. I think you can do it either way successfully, neither is wrong. It sounds like you have a better grasp of state than you probably thought 🙂

Digging a little deeper, you’ll often read about ‘stateful’ or ‘stateless’ components, and use props and functions to pass data back and forth. I’ll try to find some links for you on that subject…but I’ll give a quick explanation here:

‘Stateless’ - meaning you don’t set it to an initial state, and use props to give it a ‘state’.  
‘Stateful’ - meaning you set your component to have an initial state.

I like to break it down for myself by breaking my project into several components and then decide which ones need to have state or be stateless. Usually, I’ll have one main component that handles the state and most of the app operations. This main component also renders all of my stateless components, passing all the data to them as props. I find this an easy way for me to break it down…I’m not saying its the perfect way to do it or anything, I’m still learning too.

Hopefully that helps and makes sense.

Here’s a couple links too for you to read when you have time:

> **[Functional vs. Stateful React.js Components - ElegantCode](https://elegantcode.com/2018/01/04/functional-vs-stateful-react-js-components/)**
>
> Overview For those just getting started with React.js, it’s fairly common (in ES6 anyway) to see how to construct a React Component class and build your component from that class. It turns out we can also build React components as simple functions...

> **[Stateful vs Stateless Components](https://medium.com/@juanguardado/stateful-vs-stateless-components-444b5aa21865)**
>
> When building out your React application you will encounter the need for numerous types of Components. There will most likely be a full…

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:31am UTC](https://forum.freecodecamp.org/t/understanding-state/220524/6 "2021-02-02T11:31:20Z")

</div>

Closed on February 2, 2021, 11:31am UTC


