gpt4 book ai didi

javascript - React Material UI 1.0 在更改时选择多个参数

转载 作者:行者123 更新时间:2023-12-03 02:22:04 24 4
gpt4 key购买 nike

我正在使用 React Material Ui 1.0.0-beta.34,并且 Select 组件有问题。我正在尝试为 onChange 事件处理程序设置附加参数,但看起来只允许传递事件参数。这就是我的自定义选择组件的样子。

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Input, { InputLabel } from 'material-ui/Input';
import { MenuItem } from 'material-ui/Menu';
import { FormControl, FormHelperText } from 'material-ui/Form';
import Select from 'material-ui/Select';

class CarSelect extends Component {

render() {
const {classes, value, cars, onSelectChange} = this.props;

return(
<FormControl>
<InputLabel htmlFor="car-helper">Car</InputLabel>
<Select
value={value}
onChange={onSelectChange}
input={<Input name="car" id=car-helper />}
>
{cars.map(car => {
return (
<MenuItem key={car.carId} value={car.carId}>{car.registration}</MenuItem>
)
})}
</Select>
<FormHelperText>This is required!</FormHelperText>
</FormControl>
)
}
}

export default withStyles(styles)(CarSelect);

我的容器看起来像这样:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as carActions from '../../../redux/aircraft/carActions';
import { withStyles } from 'material-ui/styles';

class Form extends Component {
constructor(props, context) {
super(props, context)

this.state = {
form: {
car: {
carId: ''
}
}
}

this.handleSelectChange = this.handleSelectChange.bind(this);
}

componentDidMount() {
if(this.props.cars.length == 0) {
this.props.cars.loadAll();
}
}

handleSelectChange = (event) => {
let target = event.target;
let report = Object.assign({}, this.state.report);
report[event.target.name][event.target.name + 'Id'] = event.target.value;
return this.setState({report: report})
}

render() {
const { cars } = this.props;
return (
<CarSelect value={this.state.form.car.carId} cars={cars} onSelectChange={handleSelectChange} />
);
}
}

function mapStateToProps(state, ownProps) {
return {
cars: state.Car.cars
}
}

function mapDispatchToProps(dispatch) {
return {
cars: bindActionCreators(carActions, dispatch)
};
}

export default withStyles(styles) (connect(mapStateToProps, mapDispatchToProps)(Form));

因此,我尝试将其他参数传递给我的 handleSelectChange 函数,但我不知道如何传递,因为 Material UI 1.0 Select 组件回调 onChange 只传递事件对象。有人知道如何传递附加参数吗?

最佳答案

将扩展的 lambda 函数传递到 prop 中:

handleSelectChange = (event, carId) => {
// do things with event and carId
}

render() {
const { cars } = this.props;
return (
<CarSelect
value={this.state.form.car.carId}
cars={cars}
onSelectChange={(evt) => handleSelectChange(evt, this.state.form.car.carId)}
/>
);
}

关于javascript - React Material UI 1.0 在更改时选择多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49114768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com