【发布时间】:2018-03-01 18:34:58
【问题描述】:
我是 React 和 Redux 的新手,如果答案很琐碎,请原谅我,但经过广泛搜索后,我似乎找不到这个简单问题的好答案。我有多个级联选择,根据先前的选择填充数据。当用户更改所选选项时,一切正常。但是,我不知道在第一次选择中最初加载数据时如何触发 onChange 事件?这是简化的组件:
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { locationActions } from '../../_actions';
import { Input, Col, Label, FormGroup } from 'reactstrap';
class HeaderSetup extends React.Component {
constructor(props) {
debugger;
super(props);
this.state = { location: 'Select an Option'};
}
componentWillReceiveProps(nextProps) {
if (nextProps.loading !== this.props.loading &&
nextProps.success !== this.props.success &&
!nextProps.loading && nextprops.success) {
this.setState({ location: '' });
}
}
onLocationChanged(e) {
console.log(e.target.value);
}
render() {
const { locations } = this.props;
return (
<FormGroup row>
<Label for="locations" sm={3}>Locations</Label>
<Col sm={8}>
{locations.items &&
<Input type="select" name="locations" id="locations"
onChange={this.onLocationChanged}
value={this.state.location}>
{locations.items.map((location, index) =>
<option key={location.id}>
{location.locationName}
</option>
)}
</Input>
}
</Col>
</FormGroup>
)
}
}
function mapStateToProps(state) {
debugger;
const { locations } = state;
return {
locations
};
}
export default connect(mapStateToProps)(HeaderSetup);
我只需要手动触发它吗?如果是这样,最好的地方/方法是什么?非常感谢任何帮助!
【问题讨论】:
-
您的控制台日志有效吗?
-
@Omar - 当我更改选择选项时会发生这种情况。不在初始加载时。
标签: reactjs redux react-redux redux-form react-select