Home > Blockchain >  Why are not change value in react.js
Why are not change value in react.js

Time:08-07

This is my code I am using getDerivedStateFromprops() method but value get not change in render() please suggestion answer.

 class Strick_ extends React.Component {
      constructor() {
        super();
        this.state = {
          Name: "",
          Room: "",
          Fair: 0
        };
      }
      getDerivedStateFromProps(props , state){
      return {
        Name:props.Names
      }
      }
    
      render() {
        return (
          <>
            <h3>PG Details </h3>
            <p>Room Person:{this.state.Name}</p>
            <p>Room number:{this.state.Room}</p>
            <p>Room faie:{this.state.Fair}</p>
          </>
        );
      }
    }
    const root = ReactDom.createRoot(document.getElementById("root"));
    root.render(<Strick_ Names="Ankit Chauhan"/>);

CodePudding user response:

Your code is seems to be missing "static" right before getDerivedStateFromProps. Try this:

static getDerivedStateFromProps = (props, state) => {
 return { Name: props.Names };
};
  • Related