3

Is there a way to set the state in a Component with the Props the Component receives from a Parent Component?

export default class SomeComp extends Component {
    constructor(props) {
        super(props);

        this.state = someProps; // <-- I need the props to be the state of the Component
    }


    render() {
        const { someProps } = this.props;
        ...
    }
}

Or can I write a function, something like

export default class SomeComp extends Component {
    constructor(props) {
        super(props);

    }

    _setProps = (someProps) => {
          this.State = someProps;
    }

    render() {
        const { someProps } = this.props;
        this._setProps(someProps);
        ...
    }
}
4
  • 1
    first it's not a good practice of storing all the props values in state of child component, use this to solve your problem: this.state = props Aug 10, 2017 at 10:09
  • @MayankShukla I think I need more code to understand what you mean. I need to pass props from parent component to child component because a function in the child component needs to process the props from parent component in order to render itself (a list, ...). But, for more behaviour, I need the props from parend component to be written into the state of the child component
    – four-eyes
    Aug 10, 2017 at 10:14
  • 2
    inside constructor of child this.state = {keyName: props.keyName} now data in keyName from parent will get stored in state of child component. Aug 10, 2017 at 10:17
  • @MayankShukla I write the props in parent component with this.state = .... in the constructor?
    – four-eyes
    Aug 10, 2017 at 10:26

2 Answers 2

2

As Mayank Shukla mentioned, it is bad practice to store parent props in a childs state, thus managing the state within the child.

The whole idea of passing down the props to the child is, that you needn't care about state within the child, because it's all trickling down from the parent.

Child components should only care about their state.

What you should do instead (and what is good react practice) is to have the state in the parent component and pass event handlers down to the child which will change the state on the parent.

// in parent
class MyParentComponent extends React.Component {
  constructor() {
    super();
    this.state = {
      data: someData;
    };
  }

  handleChange(data) {
    this.setState(data);
  }

  render() {
    return (
      <MyChildComponent
        data={this.state.data}
        handleChange={this.handleChange}
      />
    );
  }
}



class MyChildComponent extends React.Component {
  // this is going to update the data in parent
  // and trickle it back down to the child
  this.props.handleChange({ foo: 'bar' });
}
3
  • this is true on paper but as code grows bigger there are a few use cases that contradict this statement.. for example deep stack of components or screen components that do should not hold state logic for all children and their children components etc etc.. even though they do sometimes need to set some logic on them
    – Blue Bot
    Aug 3, 2019 at 10:15
  • 1
    well at that point you probably want to look at something like redux anyways. that is what they mean when they say "don't use redux until you need to use redux".
    – flaky
    Aug 5, 2019 at 9:00
  • usually at the point you understand you "need/must" to use central state management its already way way to late and the refactoring going to be horrible.. for me.. if its something that will go to production - its central state from the get go.. learned it the hard and bloody way
    – Blue Bot
    Aug 5, 2019 at 11:38
0

The recommendation is to keep the kids states in the parent component. So the parent.state will eventually contain children section, where the kids states can be stored under unique ids.

this.state = {
     title: 'Some title',
     // other local stateful attributes
     children:{
        kidId1:{
          kidAttr1:' 'value'
        },
        ....
        kidId100:{
          kidAttr1:' 'value'
        }
     }
};
2
  • how would I access the props from parent in child component?
    – four-eyes
    Aug 10, 2017 at 10:15
  • Parent should get the kid state and pass it to kid constructor.
    – bluehipy
    Aug 10, 2017 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.