주 구성 요소 내부에 json 배열을 보관하고 있으며 입력을 보관하는 "FormComponent"를 만들었습니다.
부모 구성 요소 내부에 '추가'기능을 만들었으며 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} />
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다