gpt4 book ai didi

javascript - this.prop 没有得到正确的结果

转载 作者:行者123 更新时间:2023-12-03 03:47:22 26 4
gpt4 key购买 nike

我正在开发一个简单的待办事项应用程序来练习我的 react 技能。我有 3 个组件,我将在下面向您展示。

问题是我的 Prop 没有正确通过,因为只有当 onChange 方法设置 State 时它才被识别,我将向您展示:

这是我的 AddTodoComponent:

import React, { Component } from 'react';
import '../../App.css';
import update from 'react-addons-update';

import TodoItemComponent from '../TodoItemComponent/TodoItemComponent';

class AddTodoComponent extends Component {
constructor(props) {
super(props);

this.state = {
todo: '',
todoArray: []
};
}

addTodo(e){
console.log('e', e.target.value, 'todoArray', this.state.todoArray);
this.state.todoArray.push(e.target.value);
this.setState({ todo: this.state.todo });

// this.state.todoArray.push(<TodoItemComponent todo={this.state.todo} onDelete={this.delete}></TodoItemComponent>);
}

delete(index){
console.log('thisIndex', index);

this.setState({
todoArray: update(this.state.todoArray, {$splice: [[index, 1]]})
});
}

handleChange(e){
this.setState({ todo: e.target.value });
}

render() {
return (
<div>
<div className="input-group m-b-md">
<input type="text" className="form-control add-todo" placeholder="Todo..." value={this.state.todo} onChange={this.handleChange.bind(this)} />
<span className="input-group-btn">
<button className="btn btn-react" type="button" onClick={this.addTodo.bind(this)}> Add</button>
</span>
</div>
<ul className="list-group">
{this.state.todoArray.map((todo, index) => (
<TodoItemComponent todo={this.state.todo} key={index} onDelete={this.delete.bind(this, index)}></TodoItemComponent>
))}
</ul>
</div>
);
}
}

export default AddTodoComponent;

这是 TodoItemComponent.jsx:

import React, { Component } from 'react';
import '../../App.css';

class TodoItemComponent extends Component {
deleteTodo(todo){
console.log('deleteTodo', this.props.todo);
this.props.onDelete(this.props.todo);
}

render() {
console.log('this.props.todo', this.props.todo);

return (
<div>
<li className="list-group-item todo-item">
<button className="btn btn-xs btn-react btn-circle m-r-md">
<span className="fa fa-check"></span>
</button>
{this.props.todo}
<span className="pull-right">
<button className="btn btn-xs btn-react btn-circle m-r-xs">
<span className="fa fa-pencil-square-o"></span>
</button>

<button className="btn btn-xs btn-react btn-circle" onClick={() => this.deleteTodo(this.props.todo)}>
<span className="fa fa-trash-o"></span>
</button>
</span>
</li>
</div>
);
}
}

export default TodoItemComponent;

这里 this.props.todo 根本没有给我任何响应。我希望输入字段的值传递到 TodoItemComponent 时显示我点击添加。

最佳答案

在构造函数中调用 super(props) 并在 addTodo(e) 函数中将 this.setState({ todo: this.state.todo }) 更改为

const todos this.state.todoArray.slice();
todos.push(e.target.value); // this will insert the button state
this.setState({ todoArray: todos });

并且在渲染中而不是

{this.state.todoArray.map((todo, index) => (
<TodoItemComponent todo={this.state.todo} key={index} onDelete={this.delete.bind(this, index)}></TodoItemComponent>
))}

这样做

{this.state.todoArray.map((todo, index) => (
<TodoItemComponent todo key={index} onDelete={this.delete.bind(this, index)}></TodoItemComponent>
))}

更新:而不是 todos.push(e.target.value); 在 addTodo() 中执行 todos.push(this.state.todo);

关于javascript - this.prop 没有得到正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45324958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com