reactjs - 尝试通过道具向子级提升功能时超出最大更新深度
问题描述
我在我的主要组件中保存了一个 json 数组,并且我创建了一个保存输入的“FormComponent”。
我在父组件中创建了一个“添加”函数,我正试图通过名称下的 prop 将它发送给子组件,createTODOhander
如您在此处看到的:
import * as React from "react";
import Task from "./Task";
import FormComponent from "./FormComponent";
class TodoList extends React.Component {
state = {
MAXIMUMJOBS: 5,
TodoList: [],
}
createTODO = (Todoname) => {
this.setState({
TodoList: this.state.TodoList.concat({Todoname: Todoname})
})
}
render() {
return (
<div style={{marginLeft: '10px'}}>
<h3>You can enter {this.state.MAXIMUMJOBS - this.state.TodoList.length} TODO list jobs.</h3>
<FormComponent createTODOhandler={this.createTODO()} />
)
}
}
在FormComponent
我试图以这种方式使用它:
import * as React from "react";
class FormComponent extends React.Component {
state = {
inputValue: ''
}
inputChanged = (e) => {
this.setState({
inputValue: e.target.value
})
};
createTODO = () => {
if (this.state.inputValue === '') {}//do nothing, not gonna happen because the button is disabled.
else{
this.props.createTODOhandler(this.state.inputValue);
}
};
render() {
return (
<div>
<input type="text" onChange={this.inputChanged} value={this.state.inputValue}
placeholder={"Enter your job here."}/>
<button onClick={this.createTODO}>Add!</button>
<button>Clear list</button>
</div>
)
}
}
export default FormComponent;
但我得到一个Maximum update depth exceeded.
错误,
谢谢你的帮助。
解决方案
应该()
从道具中移除。
这个<FormComponent createTODOhandler={this.createTODO()} />
变成:<FormComponent createTODOhandler={this.createTODO} />
推荐阅读
- python - 如何对具有许多唯一值的分类变量进行热编码?
- c - 在 gcc 内联中实现原子 CAS
- azure-active-directory - 如何更改 Azure AD 中基于 SAML 的 SSO 的传出声明?
- django - 如何在请求响应周期内杀死 gunicorn 服务器
- mysql - 当值为 UNIQUE 时,INSERT INTO ... SELECT value + 1 是否需要锁定?
- appium - WebdriverIO:如果waitForDisplayed()返回false,则测试不会失败
- php - 动态依赖选择下拉php sql同表
- python - IndentationError:需要一个缩进块。不知道哪里写错了
- kivy - Kivy:如何设置相对于父类的子类属性?
- python - python中的字符串操作以提取特定字段