为什么我在调度动作时会得到“null”?

时间:2017-12-18 02:15:50

标签: reactjs redux

的console.log(bankStore.getState()平衡); 我得到0是预期的。

然后我有一个容器,它将道具传递给<BankApp />组件(据我所知):

const mapStateToProps = state => {
  return {
    balance: state.balance
  };
};
const mapDispatchToProps = dispatch => {
  return {
    onDeposit: amount => dispatch(depositIntoAccount(amount)),
  };
};
const BankAppContainer = connect(mapStateToProps, mapDispatchToProps)(BankApp);

这是我的<BankApp />组件,它从BankAppContainer接收道具:

class BankApp extends React.Component {
  state = {
    amount: 0
  };

  onInputChange = e => {
    const amount = e.target.value;
    this.setState({ amount });
  };

  onDeposit = e => {
    e.preventDefault();
    this.props.onDeposit({
      amount: parseFloat(this.state.amount, 10)
    });
    this.setState({ amount: 0 });
  };

  render() {
    return (
          <form>
            <input
              type="text"
              onChange={this.onInputChange}
              placeholder="add num"
              value={this.state.amount}
            />
            <br />
            <div>
              <br />
              <button onClick={this.onDeposit}>deposit</button>
              <button>withdraw</button>
            </div>
          </form>

这是我的depositIntoAccount的行动创建者:

export const depositIntoAccount = amount => ({
  type: "DEPOSIT_INTO_ACCOUNT",
  amount
});

这是我的app reducer:

export default (state = { balance: 0 }, action) => {
  switch (action.type) {
    case "DEPOSIT_INTO_ACCOUNT":
      return {
        balance: state.balance + parseFloat(action.amount)
      };

当我点击deposit时,我正在调度depositIntoAccount操作,但我的console.log(bankStore.getState().balance)现在是Object {balance: null}

1 个答案:

答案 0 :(得分:2)

您在行动中错误地发送了一个对象。

start_end_collate = house_max.groupby('Date_Time_max')['House_Id'].size()

这应该是

this.props.onDeposit({
  amount: parseFloat(this.state.amount, 10)
});