为什么当状态改变时我的组件没有重新渲染?

错误的惩罚

存储在道具中的值 1 和 2 和 3 的值是 Math.floor(Math.random() * 100) 所以我想知道什么时候我 setState 和 numQuestions 的值, numCorrect 改变和渲染方法是因为我知道为什么这个渲染没有使三个值 1,2,3 再次被复制并且它们的值随着两个按钮的每次单击而改变。

class App extends Component {
      constructor(props) {
      super(props)
      this.proposedAnswer = Math.floor(Math.random() * 3) + this.props.value1 + this.props.value2 + this.props.value3;
      this.state = {
        numQuestions : 0,
        numCorrect : 0,
      }
   }

   rightanswer= ()=> {
        if(this.proposedAnswer === this.props.value1 + this.props.value2 + this.props.value3){
          this.setState((oldstate)=> ({
            numCorrect : oldstate.numCorrect +=1 ,
            numQuestions : oldstate.numQuestions += 1 ,
          }))
        }else{
             this.setState((oldstate)=> ({
             numQuestions : oldstate.numQuestions += 1 ,
         }))
     }
  }

   falseanswer= ()=> {
         if(this.proposedAnswer !== this.props.value1 + this.props.value2 + this.props.value3){
           this.setState((oldstate)=> ({
               numCorrect : oldstate.numCorrect +=1 ,
               numQuestions : oldstate.numQuestions += 1 ,
           }))
         }else{
               this.setState((oldstate)=> ({
               numQuestions : oldstate.numQuestions += 1 ,
         }))
        }
      }

   render() {
      return (
          <div className="App">
            <div className="game">
              <h2>Mental Math</h2>
              <div className="equation">
                  <p className="text">{`${this.props.value1} + ${this.props.value2} + 
                   ${this.props.value3} = ${this.proposedAnswer}`}</p>
              </div>
              <button  onClick={() => this.rightanswer()}>True</button>
              <button onClick={() => this.falseanswer()}>False</button>
              <p className="text">
                 Your Score: {this.state.numCorrect}/{this.state.numQuestions}
              </p>
             </div>
          </div>
       );
      }
    }

  export default App;
辉光

当状态改变时,你的组件会重新渲染。但是,您的 props 不会改变,因为您没有从父组件传入新的 props,所以value1, value2, 并value3保持不变。

如果您希望values 在状态更改时更改,一种方法是将所有values 移动到状态,在组件安装时初始化它们,并在组件更新时更新它们:

  constructor(props) {
    super(props);
    this.state = {
      numQuestions: 0,
      numCorrect: 0
    };
  }

  componentDidMount() {
    this.generateValues();
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.state.numQuestions !== prevState.numQuestions) {
      this.generateValues();
    }
  }

  generateValues = () => {
    const values = [...Array(3)].map(() => Math.floor(Math.random() * 100));
    const correctAnswer = values.reduce((a, b) => a + b, 0);
    const proposedAnswer = Math.floor(Math.random() * 3) + correctAnswer;
    this.setState({
      value1: values[0],
      value2: values[1],
      value3: values[2],
      correctAnswer: correctAnswer,
      proposedAnswer: proposedAnswer
    });
  };

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么当 Set 状态改变时我的 React 组件没有重新渲染?

组件没有在状态改变时重新渲染?

更改状态后,为什么我的组件没有重新渲染?

当我更改状态时,为什么我的 React 应用程序没有重新渲染?

React js,当我更改open的值时,为什么我的组件没有重新渲染?

为什么我的功能组件没有重新渲染?

当第二次单击时状态没有改变时组件重新渲染

当 props 改变时,Vue 子组件没有重新渲染?

即使状态没有改变,React 组件在重新渲染时也会创建一个新的状态实例

为什么 PrivateRoute 会阻止我的 Redux 连接组件在商店状态更新时重新渲染?

为什么在状态更新时不响应组件重新渲染?

更改不相关状态后,为什么我的所有组件都会重新渲染?

如果其状态没有改变,无状态组件会重新渲染吗?

当我从父组件更改其状态时,为什么我的子组件没有更新?

仅在状态改变时重新渲染反应组件

vuex 状态改变时如何重新渲染 vue 组件?

为什么在 React 中,当父组件重新渲染时,子组件不会重新渲染(子组件没有被 React.memo 包裹)?

ReactJS - 当状态改变时,数据表没有重新渲染/更新

为什么钩子状态改变会导致组件渲染?

为什么状态没有改变?

我可以做些什么来改变状态而不重新渲染

为什么当状态改变时React Native组件不重新呈现

为什么单击时我的颜色没有改变?

React Native:组件重新渲染但道具没有改变

当我在 forEach 循环中重新分配属性时,为什么我的对象没有改变?

为什么我的ReactJS组件状态更改未触发重新渲染?

通过上下文更新单个状态时,为什么要完全重新渲染组件?

为什么我的状态没有改变,为什么我对{store.getState()}的引用根本没有出现?

为什么当我在 redux 中的这个 reducer 中的状态发生变化时,我的状态没有重新渲染