React + Redux - 在初始加载时触发 select onChange()
React + Redux - Trigger select onChange() on initial load
我是 React 和 Redux 的新手,如果答案很简单,请原谅我,但经过广泛搜索后,我似乎找不到这个简单问题的好答案。我有多个级联 selects,其中数据根据先前的 selection 填充。当用户更改 selected 选项时一切正常。但是,我不知道如何在第一个 select 中最初加载数据时触发 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);
我只需要手动触发吗?如果是这样,最好的 place/way 是什么?非常感谢任何帮助!
由于您使用的是 controlled components,因此它们应该始终反映状态。在您的 onChange
回调中,您应该只更新状态,所有输入都应该相应更新。
如果您提出一个最小的工作示例来说明这个问题,我也许可以提供更多详细信息。
下面是一个简单的设置示例:
class App extends React.Component {
state = {
locations: []
};
componentDidMount() {
setTimeout(() => { // simulate loading
this.setState({
loading: false,
locations: [
{
id: 1,
label: "Paris"
},
{
id: 2,
label: "Rome"
}
]
});
}, 3000);
}
render() {
return <MyForm locations={this.state.locations} initialLocation={2}/>;
}
}
class MyForm extends React.Component {
state = {
initialLocation: null,
location: ""
};
componentWillReceiveProps(nextProps) {
this.setState({
initialLocation: nextProps.initialLocation,
})
}
onChange = e => {
this.setState({
location: e.target.value
});
};
render() {
const { locations } = this.props;
return (
<label>
<div>Select a location:</div>
{locations.length > 0 && (
<select value={this.state.location || this.state.initialLocation} onChange={this.onChange}>
{locations.map(({ id, label }) => (
<option key={id} value={id}>
{label}
</option>
))}
</select>
)}
</label>
);
}
}
ReactDOM.render(<App />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
我是 React 和 Redux 的新手,如果答案很简单,请原谅我,但经过广泛搜索后,我似乎找不到这个简单问题的好答案。我有多个级联 selects,其中数据根据先前的 selection 填充。当用户更改 selected 选项时一切正常。但是,我不知道如何在第一个 select 中最初加载数据时触发 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);
我只需要手动触发吗?如果是这样,最好的 place/way 是什么?非常感谢任何帮助!
由于您使用的是 controlled components,因此它们应该始终反映状态。在您的 onChange
回调中,您应该只更新状态,所有输入都应该相应更新。
如果您提出一个最小的工作示例来说明这个问题,我也许可以提供更多详细信息。
下面是一个简单的设置示例:
class App extends React.Component {
state = {
locations: []
};
componentDidMount() {
setTimeout(() => { // simulate loading
this.setState({
loading: false,
locations: [
{
id: 1,
label: "Paris"
},
{
id: 2,
label: "Rome"
}
]
});
}, 3000);
}
render() {
return <MyForm locations={this.state.locations} initialLocation={2}/>;
}
}
class MyForm extends React.Component {
state = {
initialLocation: null,
location: ""
};
componentWillReceiveProps(nextProps) {
this.setState({
initialLocation: nextProps.initialLocation,
})
}
onChange = e => {
this.setState({
location: e.target.value
});
};
render() {
const { locations } = this.props;
return (
<label>
<div>Select a location:</div>
{locations.length > 0 && (
<select value={this.state.location || this.state.initialLocation} onChange={this.onChange}>
{locations.map(({ id, label }) => (
<option key={id} value={id}>
{label}
</option>
))}
</select>
)}
</label>
);
}
}
ReactDOM.render(<App />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>