【问题标题】:Show initial state in redux?在 redux 中显示初始状态?
【发布时间】:2017-11-12 20:04:18
【问题描述】:

我有一个简单的 redux/react 应用程序。它工作正常,只是它不显示初始状态(用户得分)。感谢您的帮助! 减速机如下:

  const initialState = [{userScore: 0, computerScore :0}];
 const dataReducer = (state = initialState, action) => {
 switch (action.type) {
  case 'USER_WINS':
  return [
    {
      userScore: state.userScore + 1,
      computerScore: state.computerScore

    }
  ]
  case 'COMPUTER_WINS':
    return [
      {
        userScore: state.userScore,
        computerScore: state.computerScore +1
      }
    ]
default:
  return state
}
}

主类如下:

    class Interface extends Component{
  constructor(props) {
  super(props);
  this.computerChoice = this.computerChoice.bind(this);
  this.userChoice = this.userChoice.bind(this);
}

computerChoice () {
  var choicesArray = ['Paper','Rock','Scissors'];
  var computerResponse = choicesArray[Math.floor(Math.random() * 3)];
   return computerResponse;
}

userChoice (userInput){
    var computerInput = this.computerChoice();
    if (
        (userInput === 'Rock' && computerInput === 'Scissors')||
        (userInput === 'Paper' && computerInput === 'Rock') ||
        (userInput === 'Scissors' && computerInput === 'Paper')
    ) {
         this.props.userWinsGame();
    } else {

    }
  }
  render() {
  const value  = this.props;
    return (
      <div>

这是我要显示分数的地方:

         Human: {value.userScore}
         Computer: {value.computerScore}
      <br/>
       <button value='Rock' onClick={(e) => this.userChoice(e.target.value)} >Rock</button>
      <button value='Paper' onClick={(e) =>this.userChoice(e.target.value)} >Paper</button>
      <button value='Scissors' onClick={(e) =>this.userChoice(e.target.value)} >Scissors</button>
      </div>
      )
     }
 }

一切都在这里派发:

    const render = () => ReactDOM.render(
<Interface
  value={store.getState()}
    userWinsGame = {() => store.dispatch({ type: 'USER_WINS' })}
    computerWinsGame = { () => store.dispatch({ type: 'COMPUTER_WINS' 
})}
 />,
root

)

感谢您的帮助 - 一切正常,但显示初始状态!

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    主要问题是,为什么您的 initialState 是一个数组?为什么你的 state 是一个数组?我认为您应该考虑将其保留为对象

    无论如何 - 虽然你的 initialState 是一个数组:

    const initialState = [{userScore: 0, computerScore :0}],

    您正在尝试设置 key,就好像 state 将是一个 对象

    userScore: state.userScore + 1,

    state.userScore 将返回undefined,加上1 将抛出NaN

    正如我上面所说,我强烈建议您使用 object 作为您的状态。但是,如果您真的希望它是一个数组,请使用正确的符号:

    userScore: state[0].userScore + 1,

    【讨论】:

    • 您好,感谢您的帮助!如果我将它设置为一个对象,那么 const initialState = {userScore: 0, computerScore :0};我仍然不确定如何访问其中的值!再次感谢:-)
    • @Nespony 我可以帮你解决这个问题,只要你提供一些代码来负责从状态中获取数据并显示它:) 你是否将状态映射到某个地方的道具?
    • 您好,再次感谢您!我已经添加了相关代码!
    • @Nespony 你错过了将状态映射到道具。您需要从组件内部的状态中获取userScorecomputerScore。无论如何,const value = this.props; 是不合适的,因为props 可能包含比您的state 更多的字段。它应该类似于:const value = this.props.value.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2021-06-04
    • 2018-06-08
    相关资源
    最近更新 更多