By good_evening


2019-07-11 19:00:08 8 Comments

The usual solution to updating parent is to pass the method from parent to the child, and then from child use props.method. However, parent class becomes too huge in my case, so I was wondering are there any disadvantages doing this:

<input value={message.sometext}
       onChange={(e) => {
         message.sometext = e.target.value;
         self.props.forceUpdate();
       }}/>

?

By using this, I can move logic to the child and have skinnier parent. Or forceupdating the whole state is a bad idea? Why, or why not? I am new to React, so sorry for a potentially dumb question. Thanks a lot.

1 comments

@Dupocas 2019-07-11 19:51:37

No, forcing an update on the child will not trigger an update on the parent. forceUpdate is only used when you are reading a value inside render that's not part of state or props, cause when this situation occurs React doesn't know when to trigger an update, and yet isn't a good idea, you should always read only from state or props to avoid unpredictable behaviors. You could either centralize the state (using Redux or similar), make your child component stateful(usually not a good ideia) or pass the parent's updater function to child (isn't pretty but works just fine).

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How to update parent's state in React?

0 Answered Questions

2 Answered Questions

[SOLVED] Passing store state as props, or each component accessing global stores?

2 Answered Questions

React - set state on unmounted component

1 Answered Questions

[SOLVED] How to call setState method of a parent component after updating child component's state?

  • 2018-08-10 11:08:45
  • ghazanfar malik
  • 111 View
  • 0 Score
  • 1 Answer
  • Tags:   reactjs

1 Answered Questions

Child component does not update when parent state changes

2 Answered Questions

[SOLVED] Performance on PureComponent vs stateless functional component

1 Answered Questions

1 Answered Questions

[SOLVED] What is the proper way to handle React state and props

  • 2016-04-01 18:34:44
  • André Moretzsohn de Castro
  • 180 View
  • 0 Score
  • 1 Answer
  • Tags:   reactjs render

Sponsored Content