ankit chauhan
ankit chauhan

Reputation: 5

Why are not change value in react.js

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"/>);

Upvotes: 0

Views: 74

Answers (1)

Jason
Jason

Reputation: 279

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

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

Upvotes: 1

Related Questions