How are props and state different?

Technology CommunityCategory: React NativeHow are props and state different?
VietMX Staff asked 3 years ago
  • props : are immutable and are set by the parent and they are fixed throughout the lifetime of a component.
  • state : is mutable. This means that state can be updated in the future while props can’t. we can initialize state in the constructor, and then call setState when we want to change it.

Some other things to consider:

  • Use props to pass data and settings through the component tree.
  • Never modify this.props inside of a component; consider props immutable.
  • Use props to for event handlers to communicate with child components.
  • Use state for storing simple view state like wether or not drop-down options are visible.
  • Never modify this.state directly, use this.setstate instead.