简体   繁体   English

为什么不改变 react.js 中的值

[英]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.这是我正在使用 getDerivedStateFromprops() 方法的代码,但值在 render() 中没有变化,请建议回答。

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

Your code is seems to be missing "static" right before getDerivedStateFromProps.您的代码似乎在 getDerivedStateFromProps 之前缺少“静态”。 Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM