반응하여 일 목록에서 항목을 제거하기 위해, 나는 볼 수 있습니다 배열은 내가 배열의 목록에서 제거 제거 할 항목 제대로 업데이트되지만 돔 단지 내가 대신 전체 배열방법
import React from 'react';
import ReactDom from 'react-dom';
class TodoList extends React.Component{
constructor(props){
super(props);
this.state={
todoList:[],
todo:''
}
};
onValueChange=(e)=>{
const todo=e.target.value;
this.setState({todo})
};
removeItem=(props)=>{
this.setState({todoList:this.state.todoList.splice(props,1)})
console.log(this.state.todoList)
};
onSubmit=(e)=>{
e.preventDefault();
const {todoList,todo}=this.state
this.setState({todoList:[...todoList,todo]})
this.setState({todo:''})
console.log(this.state.todoList)
};
render(){
const myList=this.state.todoList.map((todo,index)=>(
<li key={index}>
{todo}
<button onClick={()=>this.removeItem(index)}>x</button>
</li>
))
return (
<div>
<form onSubmit={this.onSubmit}>
<input
type="text"
value={this.state.todo}
onChange={this.onValueChange}
autoFocus
placeholder='todo'
/>
</form>
<ol>
{myList}
</ol>
</div>
)
};
};
ReactDom.render(<TodoList/>,document.getElementById('app'));
이의 제거 할 항목을 렌더링 사진
그림에서 당신은 C 것을 알 수 있습니다 onsole 제거 '5'항목을 가지고있는 배열을 보여 주지만, 화면 만 항목을 보여 '5'대신 항목 (1)의 4
확인이 https://stackoverflow.com/questions/43893508/remove-items-form-the-list-in-react-js/43893804#43893804 –
스플 라이스는 제거 된 항목을 반환합니다. splice를 먼저 실행 한 다음 – cdoshi
배열을 console.log로 지정하지 마십시오! setState는 비동기 메서드이며 setState 바로 다음에 상태 변경을 볼 수 없습니다. componentDidUpdate 기능에 당신의 setTimeout과 같은 방법을 사용할 수 있습니다 (() => CONSOLE.LOG (this.state)) 또는 인쇄 상태입니다. –