reactjs - reset On 解决在 React 中循环的课程
问题描述
我正在尝试将依赖值更改时的值重置为 SelectInput。但它当然会循环和打破页面。在这里找到我到目前为止所做的代码。
如何在我的代码中省略它。
import {
SelectInput,
required
} from 'react-admin';
import data from '../data';
import { withStyles } from '@material-ui/core/styles';
import React, { Component } from 'react';
import { DependentInput } from 'aor-dependent-input';
const initialState = {
way_of_join: data.way_of_join
};
class WayOfJoinSelectInput extends Component {
constructor(props) {
super(props)
this.state = initialState;
}
reset(){
this.setState({initialState});
}
switchSector = (props) => {
if (props !== undefined && Object.keys(props).length > 0) {
var value = props.General_Service.service_sector;
this.reset();
switch (value) {
case 'sleas':
this.state.way_of_join.splice(4, 3)
break;
case 'sltes':
this.state.way_of_join.splice(2, 1)
break;
}
}
};
render() {
return (
<DependentInput resolve={this.switchSector}>
<SelectInput
source="General_Service.way_join"
label="Way of Join"
validate={required()}
// onChange={this.reset()}
choices={this.state.way_of_join}
/>
</DependentInput>
)
}
}
export default withStyles(styles)(WayOfJoinSelectInput);
在这里找到什么错误。
解决方案
import {
SelectInput,
required
} from 'react-admin';
import data from '../data';
import { withStyles } from '@material-ui/core/styles';
import React, { Component } from 'react';
import { DependentInput } from 'aor-dependent-input';
const initialState = {
way_of_join: data.way_of_join
};
class WayOfJoinSelectInput extends Component {
constructor(props) {
super(props)
this.state = initialState;
this.reset=this.reset.bind(this);
}
reset(){
this.setState({initialState});
}
switchSector = (props) => {
if (props !== undefined && Object.keys(props).length > 0) {
var value = props.General_Service.service_sector;
this.reset();
switch (value) {
case 'sleas':
this.state.way_of_join.splice(4, 3)
break;
case 'sltes':
this.state.way_of_join.splice(2, 1)
break;
}
}
};
render() {
return (
<DependentInput resolve={this.switchSector}>
<SelectInput
source="General_Service.way_join"
label="Way of Join"
validate={required()}
onChange={this.reset}
choices={this.state.way_of_join}
/>
</DependentInput>
)
}
}
export default withStyles(styles)(WayOfJoinSelectInput);
您需要在构造函数中绑定函数或使用箭头函数。
推荐阅读
- python-2.7 - 如何在 web2py DAL 中的查询上应用用户定义的函数?
- php - 尝试在 CakePHP 中修改用户访问权限
- python-3.x - Unable to assign values to a pandas data frame column from a list suing iteration
- java - How to config the default version of maven-site-plugin for all project?
- dask - 设置 dask 工作人员数量的最佳实践
- c# - 在 C# 中使用 PowerShell 处理 CimObjects
- compilation - 显示先前编译的 Clion 控制台
- javascript - 异步等待在 express.js 中没有响应 mysql.js
- go - 使用 revel 和 dep 进行依赖管理
- html - 背景图像:保留浏览器窗口的纵横比