React-Redux _self用法

时间:2018-07-14 06:20:59

标签: react-redux

这是黑客吗?我正在使用_self变量来访问从容器导入的操作并到达reducer的操作。这是因为我注意到无法在handleInputChange事件中访问this.props。我还应该如何调用onChange然后该动作更像是redux方式吗?这么胖是我使它起作用的唯一方法,但我觉得这不好:-)

import * as React from 'react'
import PropTypes from 'prop-types'

let _self;
class CountrySelection extends React.Component {
  componentWillMount() {
    _self = this;
  }
  render() {
    return (
      <div>
        <div className="row">
          <label htmlFor="country">select a country</label>
          <select defaultValue={this.props.selectedCountry} 
            id="country" onChange={this.handleInputChange}>
            {
              this.props.available.map(x =>
                <option key={x.code} value={x.code}>{x.name}</option>)
            }
          </select>
        </div>
        <div className="row">
          <label htmlFor="city">select a city</label>
          <select defaultValue={this.props.selectedCity} 
            id="city" onChange={this.handleCityChange}>
            {
              this.props.selectedCities.map(x =>
                <option key={x.name} value={x.name}>{x.name}</option>)
            }
          </select>
        </div>
      </div>
    )
  }
  handleInputChange(event) {
    const target = event.target;
    const value = target.value;
    _self.props.loadCities(value);
  }
  handleCityChange(event) {
    const target = event.target;
    const value = target.value;
    _self.props.storeCity(value);
    const place = _self.props.selectedCities.filter(function(x) {
      return x.name == value;
    })[0];
    _self.props.showWeather(place.lat, place.lng);
  }
}

CountrySelection.propTypes = {
  available: PropTypes.array,
};

export default CountrySelection

1 个答案:

答案 0 :(得分:0)

我发现我应该使用()=> action(),幸运的是我只打了一个动作