Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why props in React are read only?

The React documentation says: React is pretty flexible but it has a single strict rule: all React components must act like pure functions with respect to their props.

Why is that?

I guess that if you change directly the value of the props, the component does not re-render, that's why we must use setState. But I still don't understand the reason behind this. Why components must be like pure functions with respect to their props?

like image 312
L. Pier Roberto Avatar asked Jul 20 '18 05:07

L. Pier Roberto


People also ask

What does this mean props are read only?

Props are read only means you can't change the value, but it doesn't mean you can't update it, for that you need to pass updated props back to child component and go any lifecycle methods to it.

Can props be updated or read only?

“Props” is a special keyword in React, which stands for properties and is being used for passing data from one component to another. Furthermore, props data is read-only, which means that data coming from the parent should not be changed by child components.

Why props are immutable in React?

Props are immutable, while state is mutable Although a component is not allowed to change its props, it is responsible for the props of its child components down the component tree. On the other hand, state is mutable. A stateful component changes its state every time users interact with the app.

How props are passed in React?

Props are arguments passed into React components. Props are passed to components via HTML attributes. props stands for properties.


1 Answers

The important concept of React component: a component should only manage its own state, but it should not manage its own props.

In fact, props of a component is concretely "the state of the another component (parent component)". So props must be managed by their component owner. That's why all React components must act like pure functions with respect to their props (not to mutate directly their props).

I will show you a simple example:

class ParentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      p1: {a:1, b:2},
    }

   render() {
      return <ChildComponent p1={this.state.p1} />     
   }
}

In the ChildComponent, if you want to mutate the "passed prop p1" (p1 is an object with his own ref) (ex. in the ChildComponent, you write: p1.a=3), so evidently, "the p1 - property of the state of ParentComponent" is also mutated. But the ParentComponent couldn't re-render in this case because you didn't trigger the action setState() in ParentComponent. So it will generate many uncontrolled bugs for a React App unstable.

I hope right now that you can understand why React says:

The strict rule: all React components must act like pure functions with respect to their props (not to mutate directly their props).


Bonus: for correctly changing (mutating) the props, you must use "callback fnc prop" in ChildComponent. Right now, it respects well the concept of React Component.

class ParentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      p1: {a:1, b:2},
  }

  this.changeP1 = () => {
     this.setState({p1: {a:3, b:4}});
  }

   render() {
      return <ChildComponent p1={this.state.p1} changeP1={this.changeP1} />     
   }
}
like image 191
SanjiMika Avatar answered Oct 08 '22 09:10

SanjiMika