【问题标题】:When pressed button in react, console log shows the result but page does not在反应中按下按钮时,控制台日志显示结果但页面不显示
【发布时间】:2020-05-05 21:07:14
【问题描述】:

我是 react 和 web 新手。调用函数控制台时出现问题,但无法在我的页面中显示。

            usePriest = (evt, id) => {
            const num = 3;

            const { rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards } = this.state;

            var whichPlayer;
            switch(num){
              case 1:
                whichPlayer = player1[0];
                rear[1]=whichPlayer;
                rear[0]=card6;
                break;
              case 2:

                whichPlayer = player2[0];
                bottom[1]=whichPlayer;
                bottom[0]=card6;
                break;
              case 3:
                whichPlayer = player3[0];
                bottom2[1]=whichPlayer;
                bottom2[0]=card6;
                break;
              case 4:
                whichPlayer = player4[0];
                bottom3[1]=whichPlayer;
                bottom3[0]=card6;
                break;
            }
                // bottom[1]=whichPlayer;
                // bottom[0]=card6;
                // console.log(bottom);

          }

我在这里调用我的函数


else if (mycards[0]==="/static/media/priest.ae71698d.jpg") {
          return ( <div>
            <button className="button_card1_use" onClick={(evt) => this.usePriest(evt, 1)}>Use</button>
                  <button className="button_card1_discard">Discard</button>
            <div className="about1"><p>Priest</p>
Player is allowed to see another player's hand.         </div></div>
        )
    } 

我应该在函数中返回什么或做什么才能在页面上显示它。

【问题讨论】:

  • 您不能改变状态并期望 react 检测到状态已更改,因为 react 使用 prefState !== currentState 检测状态更改
  • 我会看看react state。您可以将所需的值存储在 react state 中,然后通过调用 this.state.yourvar 将其显示在您的页面上。
  • @Michael 是的,先生。我正在这样做,但是它没有显示任何内容。
  • 好吧,我的错,我只是没有在您的代码中列出的任何地方看到它。

标签: html reactjs react-native


【解决方案1】:

您只需要在函数末尾调用setState 以反映更改 - :

usePriest = (evt, id) => {
        const num = 3;

        // your old code
        this.setState({ rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards })

      }

实际上,在 JS 中,事物是通过引用来复制的,因此我们实际上是在直接改变状态。这是相同的更好的解决方案-:

usePriest = (evt, id) => {
        const num = 3;

        const { rear,bottom2,bottom3,bottom,player1, player2, player3, player4, mycards } = this.state;

        var whichPlayer;
        switch(num){
          case 1:
            this.setState({whichPlayer: player1[0], rear: [...rear, 0: card6, 1: whichPlayer]});
            break;
          case 2:
            this.setState({whichPlayer: player2[0], bottom: [...bottom, 0: card6, 1: whichPlayer]});
            break;
          case 3:
            this.setState({whichPlayer: player3[0], bottom2: [...bottom2, 0: card6, 1: whichPlayer]});
            break;
          case 4:
            this.setState({whichPlayer: player4[0], bottom3: [...bottom3, 0: card6, 1: whichPlayer]});
            break;
        }
   }   

【讨论】:

  • 仍然改变状态:rear[1]=whichPlayer 如果 OP 使用纯组件并且 state.rear 被传递给它,那么该组件将不会呈现。你不应该改变状态。
  • 是的,但他似乎是 React 的新手。将编辑答案以获得更优化的解决方案。谢谢@HMR
  • mutate 是什么意思?其实它按我的意愿工作,我只是想知道它的副作用,未来会发生什么
猜你喜欢
  • 2021-08-05
  • 2019-08-07
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多