# Transfering states in react

**URL:** <https://forum.freecodecamp.org/t/transfering-states-in-react/443804>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 1, 2021, 4:40pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804 "2021-02-01T16:40:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 1, 2021, 4:40pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/1 "2021-02-01T16:40:23Z")

</div>

Hello

Can I share or transfer one component state or variable to another component in react?

regards,

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 1, 2021, 4:51pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/2 "2021-02-01T16:51:22Z")

</div>

Yes, put the state in a parent component of both then it is available to both of them.

> **[Lifting State Up – React](https://reactjs.org/docs/lifting-state-up.html)**
>
> A JavaScript library for building user interfaces

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 1, 2021, 4:52pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/3 "2021-02-01T16:52:44Z")

</div>

Yes, there are standard practices for passing data around. I made a [pen](https://codepen.io/kevinsmithwebdev/pen/wjXEOx) to give an example.

That can get a bit onerous with a big app, which is where things like context and redux can come in handy.

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 2, 2021, 11:34pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/5 "2021-02-02T23:34:15Z")

</div>

Kevin in your example, in child 2 there is a method:

```auto
handleButton(action) {
    this.props.handleAction(action)
  }

```

Can you please explain about it?  
I’m really confused!

thanks,

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 2, 2021, 11:43pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/6 "2021-02-02T23:43:31Z")

</div>

In the JSX for Parent, I have:

```auto
<Child2 handleAction={this.handleAction} /> 

```

Here, (right side of the equation) I am passing in _this.handleAction_ , a class method in Parent. It is being passed in (on the left side) in the prop “handleAction”. I gave the prop the same name, but I didn’t have to.

So, that prop will “magically” show up in Child2 as a prop.

Then, in the class method of Child2 called _handleButton_, I call that function.

Is that clear?

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 12:11am UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/7 "2021-02-03T00:11:42Z")

</div>

Yes. I’m trying to understand it. But really confused! 🤔

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 12:15am UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/8 "2021-02-03T00:15:11Z")

</div>

Yes, coding is confusing. And when you first start doing React, that is a whole other level of confusion.

What specifically is confusing?

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 12:45am UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/9 "2021-02-03T00:45:06Z")

</div>

This is not my first project. The lifting up is confusing!  
Can you please write comment for your code?

thanks,

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 1:40am UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/10 "2021-02-03T01:40:05Z")

</div>

It might be a little simpler if you ask a specific question.

Yes, lifting up state is weird at first. It can also get very messy in large projects (the reason we have things like context or redux). Just keep at it. Maybe look up some tutorials. But ultimately this is just an application of basic React: If two components need the same state, it needs to be maintained in a common ancestor. You already know (I assume) how to pass values to children. The weird part (for many) is passing the callback functions to manipulate the state of the ancestor. That’s why I wrote that pen. But it seems a little outdated - maybe I’ll try to tighten it up a bit, but I don’t know when I’d have the time.

In the mean time, make a fork of that pen and mess with it. See if you can understand what it is doing.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 1:49am UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/11 "2021-02-03T01:49:05Z")

</div>

I took a quick look and I feel like there are a lot of comments.

But yeah, I remember how confusing React was when I started. Do you have a specific line of code that is confusing? Or a specific concept?

---

<div class="post-metadata">

**Author:** ![nomnomcookie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nomnomcookie/32/174915_2.png) [@nomnomcookie](https://forum.freecodecamp.org/u/nomnomcookie)\
**Post date:** [February 3, 2021, 1:16pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/12 "2021-02-03T13:16:12Z")

</div>

If you want to make it more confusing use context api or redux so siblings can share state. Although context api is easier than redux.

Parent state → children component will do

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 3, 2021, 6:51pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/13 "2021-02-03T18:51:36Z")

</div>

That’s exactly the same thing though. You don’t share state between siblings, you lift state up to a parent component. The difference is that you don’t access it via props.

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 7:23pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/14 "2021-02-03T19:23:50Z")

</div>

I just keep reading and reading from yesterday. 😐

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 9:13pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/15 "2021-02-03T21:13:42Z")

</div>

Again, what specifically is confusing you?

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 9:16pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/16 "2021-02-03T21:16:50Z")

</div>

All the code! I think can’t understand the concept behind the code! Also, I’m reading and trying to understand!

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 9:57pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/17 "2021-02-03T21:57:32Z")

</div>

Can you please inform me what kind of statement is this?

```auto
handleAction = action => {
}

```

thanks,

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 10:23pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/18 "2021-02-03T22:23:05Z")

</div>

> [@lingo1357](#):
>
> ```auto
> handleAction = action => {
> }
> 
> ```

Sure, that is a specific question. I am just defining a method here. There are two ways to define a class method. The standard way is to define it as a standard function:

```auto
  doSomething1() {
    alert('hey 1')
  }

```

That may be what you’re used to, what is usually taught in React. The “newer” way is to define it as an arrow function and store it in a class property:

```auto
  doSomething2 = () => {
    alert('hey 2')
  }

```

By “newer” I don’t necessarily mean better. I tend to like the arrow function method because arrow functions don’t create their own “this” so I don’t have to worry about binding “this”. But both work. [Here](https://codepen.io/kevinsmithwebdev/pen/wjXEOx?editors=1010) is a pen where you can mess around with it. Notice that I have to bind “this” for the first method but not the second.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 10:25pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/19 "2021-02-03T22:25:28Z")

</div>

Said another way, I could have also done:

```auto
  handleAction(action) {
    // ...
  }

```

and it would have done the same thing, except that I’d have to bind “this”, for example by putting `this.handleAction = this.handleAction.bind(this)` in the constructor.

---

<div class="post-metadata">

**Author:** ![lingo1357](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lingo1357/32/183540_2.png) [@lingo1357](https://forum.freecodecamp.org/u/lingo1357)\
**Post date:** [February 3, 2021, 10:29pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/20 "2021-02-03T22:29:10Z")

</div>

> [@lingo1357](#):
>
> ```auto
> handleAction = action => {
> }
> 
> ```

You mean they are the same as each other:

```auto
handleAction = action => {
}

```

```auto
doSomething2 = () => {
    alert('hey 2')
  }

```

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 3, 2021, 10:35pm UTC](https://forum.freecodecamp.org/t/transfering-states-in-react/443804/21 "2021-02-03T22:35:12Z")

</div>

I am saying that:

```auto
handleAction = action => {
}

```

and

```auto
handleAction(action) {
}

```

Would be essentially the same, as long as you remember to bind “this” for the second one.

The equivalent of:

```auto
doSomething2 = () => {
  alert('hey 2')
}

```

would be

```auto
doSomething2() {
  alert('hey 2')
}

```

But again, since it is a standard function/method, you have to bind “this”. Although, in this case since _this_ is not needed inside the function, there is actually no need to bind it.

Mess around with [this pen](https://codepen.io/kevinsmithwebdev/pen/wjXEOx?editors=1010) a little and see if it makes more sense.

[Next page](https://forum.freecodecamp.org/t/transfering-states-in-react/443804.md?page=2)
