[英]edit todo list state not passed correctly in react with axios
I have a component that lists the current todo's which you can add your own todo's and delete todo's, which is working 100%. 我有一个列出当前待办事项的组件,您可以添加自己的待办事项并删除待办事项,这是100%有效的。 The only issue I am facing is updating my current todo's.
我面临的唯一问题是更新当前待办事项。 I have added the necessary code below, help would be appreciated.
我在下面添加了必要的代码,将不胜感激。
I am using proptypes and can see I have warning in my console which I suspect might be the problem. 我正在使用原型,可以在控制台中看到警告,我怀疑这可能是问题所在。 Here is the warning:
这是警告:
Warning: Failed prop type: The prop `editTodo` is marked as required in `TodoItem`, but its value is `undefined`.
in TodoItem (at Todos.js:10)
in Todos (at App.js:83)
in section (at App.js:82)
in Route (at App.js:75)
in main (at App.js:73)
in div (at App.js:72)
in Router (created by BrowserRouter)
in BrowserRouter (at App.js:71)
in App (at src/index.js:7)
Here is my edit todo button: 这是我的编辑待办事项按钮:
<div id="card-item-edit">
<button
className="edit-btn"
onClick={() => this.toggleForm()}>
EDIT
</button>
{this.showEditTodoForm()}
</div>
In my edit todo button I have assigned a toggle function onClick which opens input field if true: 在我的编辑待办事项按钮中,我分配了一个切换功能onClick,如果为true,它将打开输入字段:
toggleForm = () => {
if (!this.state.isShowing) {
this.setState({ isShowing: true });
} else {
this.setState({ isShowing: false });
}
}
Which passes state and opens this form onClick 传递状态并打开此表单onClick
showEditTodoForm = () => {
if(this.state.isShowing) {
return(
<div>
<form onSubmit={this.handleFormSubmit}>
<input
type="text"
name="edit todo"
placeholder="Edit Your Todo"
value={this.state.value}
onChange={this.onChange}
/>
</form>
</div>
)
}
}
onSubmit the value is updated with Axios. onSubmit值将使用Axios更新。 I think I might be doing something wrong here, I tried testing with Postman but just can't get it working, here is my handleFormSubmit function:
我想我可能在这里做错了,我尝试使用Postman进行测试,但无法正常工作,这是我的handleFormSubmit函数:
handleFormSubmit = (id) => {
const title = this.state.title;
event.preventDefault();
axios.put(`http://localhost:3004/todos/${id}`,
{
title
},
)
.then(() => {
})
.catch(error => console.log(error))
}
I am also using the onChange property in the form submit, here is the function: 我也在表单提交中使用onChange属性,这里是函数:
onChange = (e) =>
this.setState({
[e.target.name]: e.target.value // demo react tools to show what happens when value changes when typing
}
);
Managed to resolve this with online mentor from Codementor, would highly recommend this resource for anyone stuck with problems like these. 设法通过Codementor的在线指导者解决了这个问题,强烈建议将此资源推荐给遇到此类问题的任何人。 Here is the solution:
解决方法如下:
Edit todo form, using ref's to pass state: 编辑待办事项表单,使用ref传递状态:
showEditTodoForm = () => {
const { title} = this.props.todo
if(this.state.isShowing) {
return(
<div>
<form ref={this.formRef} onSubmit={this.handleFormSubmit}>
<input
type="text"
name="title"
placeholder="Edit Your Todo"
defaultValue={title}
/>
<input type="submit" value="Save" />
</form>
</div>
)
}
}
handleFormSubmit = (e) => {
e.preventDefault()
const title = this.formRef.current['title'].value
const { id } = this.props.todo
this.props.editTodo(id, title)
}
Then I am using proptypes to pass to my main component: 然后,我使用proptypes传递给我的主要组件:
editTodo = (id, title) => {
axios.put(`http://localhost:3004/todos/${id}`,
{
title
},
)
.then(({data}) => {
this.setState(prevSate => {
const { todos } = prevSate;
const oldTodoIndex = todos.findIndex(todo => todo.id === data.id )
const newTodo = {...todos[oldTodoIndex], ...data}
todos.splice(oldTodoIndex, 1, newTodo)
return {todos: todos}
})
})
.catch(error => console.log(error))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.