reactjs - 禁用反应开关
问题描述
我是新来的反应并试图禁用地图功能中的反应开关按钮。它应该在用户在输入文本字段中输入键后启用。我试图在 componentWillUpdate 上启用它,但它说超出了最大调用堆栈大小。
constructor(props){
super(props)
this.state ={
keyValues : '',
tokenValues : '',
urlValues : '',
checked : false,
displayDetails : false,
disableSwitch : true
}
this.toggleFunction = this.toggleFunction.bind(this);
}
这是我当地的州。
toggleFunction = (id ) =>{
let data =[];
let loggedInUserEmail = JSON.parse(localStorage.loggedInUserDetails).email;
data.push({
projectId : id,
keyValues : this.state.keyValues,
urlval : this.state.urlValues,
tokens : this.state.tokenValues,
email : loggedInUserEmail
})
if(this.state.keyValues !== ''){
this.props.dispatch(abc(data))
}
}
这是我的切换功能。
<Table cellpadding="5px" cellspacing="10px" type = 'SETTINGS_JIRA' tableClass = 'settings-jira-table'>
{listOfProj.map((item , index) => {
return(
<Row>
<Cell key = {index} cellClass = 'settings-jira-projects' > {item.get('projectTitle').toUpperCase()} </Cell>
<Cell> <input type='text' onChange = {(e) => this.setState({keyValues:e.target.value })} className ='settings-jira-textfield' /> </Cell>
<Cell> <input type='text' onChange = {(e) => this.setState({tokenValues : e.target.value})} className ='settings-jira-textfield' /> </Cell>
<Cell> <input type='text' onChange = {(e) => this.setState({urlValues : e.target.value})} className='settings-jira-textfield' /></Cell>
<Cell >
<label htmlFor="material-switch">
<Switch
checked={this.state.checked}
onChange ={() => {this.toggleFunction(item.get('projectId'))}}
onColor="#86d3ff"
onHandleColor="#2693e6"
handleDiameter={25}
uncheckedIcon={false}
checkedIcon={false}
boxShadow="0px 1px 5px rgba(0, 0, 0, 0.6)"
activeBoxShadow="0px 0px 1px 10px rgba(0, 0, 0, 0.2)"
height={17}
width={45}
className="react-switch"
id="material-switch"
disabled= {this.state.disableSwitch}
/>
</label>
</Cell>
</Row>
)
})}
</Table>
尝试在生命周期组件中启用它
componentWillUpdate(){
if(this.state.keyValues !== ''){
this.setState(prevState => ({
disableSwitch : !prevState.disableSwitch
}))
}
}
谢谢!
解决方案
当我们调用this.setState
它调用的方法时componentWillUpdate
,这就是您的组件进入递归状态的方式,如果我们触发状态更改,componentWillUpdate()
我们将最终陷入无限循环。我认为您需要更好地办理登机手续componentWillUpdate
推荐阅读
- php - PHP MySQL PDO数组绑定不返回任何结果
- apache - vue 应用程序/浏览器显示旧数据
- wordpress - 错误 400:invalid_request 提示参数值无效:无效提示:缺点
- angular - @angular/fire 导致 Illegal App name: '[object Object] (app/bad-app-name)
- cross-compiling - 我不能用 nvcc 编译如何在 linux 上为 x64 窗口 nvcc 编译 exe?
- reactjs - 如何使用日期输入类型将日期保存在 Firestore 中?
- networking - 使用 XDP 在点击之间重定向
- c - 为什么我会得到这些看似随机的输出?
- c++ - 如何将指向字段的指针与指向结构的指针分开?
- c - 指针(char*)与整数错误的比较