在handleChange中设置状态

时间:2019-02-09 15:02:25

标签: reactjs redux

我正在尝试将状态设置为下拉列表中的当前选择项。我认为这样做会比遍及整个行动/减速器链更容易。但是在这种情况下,setState将始终为空,并且将永远不会调用componentWillReceiveProps

有没有办法做到这一点?

  componentWillReceiveProps(nextProps) {
    console.log("collection next props", nextProps);
    this.setState({
      currentList: nextProps.currentList,
    });
  }

  handleChange (e) {
    console.log('handle change called', e.target.value); //Value is received
    //this.setState({value: e.target.value});
    this.setState({currentList: e.target.value}); //This is never getting the value
    this.props.fetchlistOfAssets(e.target.value); //This action executes without problem

  };

  render() {
    /* Read from market and last price */
    const postItems = this.props.indexes.map(post => (

      <option key={post} value={post}>{post}</option>
    ));
    return (
      <div>
        <h1>Select index</h1>
        <select name="index" onChange={this.handleChange}>{postItems}</select>
      </div>
    );
  }
}

编辑添加连接和mapState

const mapStateToProps = state => ({
  indexes: state.posts.indexdays,
  currentList: state.posts.currentList
});

export default connect(
  mapStateToProps,
  { fetchIndexDays, fetchlistOfAssets }
)(CollectionDropDown);

0 个答案:

没有答案