Get state of a child component

All we need is an easy explanation of the problem, so here it is.

I am using react-draggabe. I like the idea of adding functionality to a child component by wrapping it into another like <Draggable> does.
This component manages its own state which is cool. This way I dont have to care about anything.

However: I need to get the current position of the dragged element when an event occurs (e.g. user clicks a button somewhere in another child component).
I know I could use the onStop-handler and save the new position in the state of the parent elemet. But thats a problem: I duplicate the state of the draggable component because I also save it in the parent. I dont want to do that.

What is the react-way of doing this?
I am new to rect and this thinking so I would be thankful for a good explanation.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can do it with refs:

var Foo = React.createClass({       
  componentDidMount() {
    console.log( // "qux"  
  render() {
    return (
        <Bar ref="bar"  />

var Bar = React.createClass({
  getInitialState: function() {
    return {baz: 'qux'};

I’ve made a fiddle for you with an example of accessing state form a child component. There one component increments a value, and it’s sibling component renders it, accessing this value via props/refs from their parent component.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply