如何在onChange上分配值

时间:2018-06-25 11:06:50

标签: reactjs redux dispatch

我正在构建一个搜索组件,并且已经设置了动作和reducer等等……但是我不知道如何在我的组件中调度事件。 onChangeValue属性中应该包含什么?

代码如下:

import React from "react";
import { connect } from "react-redux";
import { getListOfUsers, clearDetails } from "../../actions/actions";
import SearchBar from "../../components/search/search";

const SearchBarContainer = onChangeValue => {
  return <SearchBar onChangeValue={onChangeValue} id="search" icon="search" />;
};

const mapStateToProps = state => {
  return {};
};

const mapDispatchToProps = dispatch => {
  return {
    onChangeValue: e => {
      dispatch(getListOfUsers(e.target.value));
      dispatch(clearDetails());
    }
  };
};

const connected = connect(
  mapStateToProps,
  mapDispatchToProps
)(SearchBarContainer);

export default connected;

2 个答案:

答案 0 :(得分:2)

SearchBarContainer是一个功能组件,因此不会包含statethis变量。您需要从道具中获取它们。另外,分派功能onChangeValue可以作为容器的道具。

const SearchBarContainer = ({ onChangeValue, value }) => {
  return (
    <SearchBar
      onChangeValue={onChangeValue}
      value={value}
      id="search"
      icon="search"
    />
  );
};

答案 1 :(得分:0)

更改

<SearchBar
      onChangeValue={onChangeValue}
      value={this.state.value}
      id="search"
      icon="search"
    />

<SearchBar
          onChangeValue={this.props.onChangeValue}
          value={this.state.value}
          id="search"
          icon="search"
        />