【发布时间】:2018-11-13 22:36:20
【问题描述】:
您好,现在正在尝试学习 React,但仍处于起步阶段。 我在 codepen 中编写了下面的代码(请参阅底部的链接),在我的代码中,我将一些日志放在控制台语句中?
我认为它无法访问它,因为我可以打印到 'let current_todos = this.state.todos' 上方的控制台文本,但我从未在控制台文本中看到它的下方。
如果这是不正确的,那么我应该如何访问状态? 注意:我意识到该函数中的许多代码是多余的,但我声明了这些变量和日志语句以用于调试目的
class TodoApp extends React.Component {
constructor(props) {
super(props)
this.state = {
todos : [ ]
}
}
render() {
return (
<div className='todo-comp todo-app'>
<h2>ToDo App</h2>
<form onSubmit={this.handleSubmit}>
<input type="text">
</input>
</form>
<TodoList todos={this.state.todos}/>
</div>
)
}
handleSubmit(event) {
let new_todo = event.target.children[0].value
console.log("Submited: ".concat(new_todo))
let current_todos = this.state.todos
console.log("Succesfully accessed state")
this.setState({"todos" : this.state.todos.push(new_todo)})
}
}
class TodoList extends React.Component {
constructor(props) {
super(props)
}
render () {
return (
<ul className="todo-comp todo-list">
{this.props.todos.map(
function(item,key) {
return(
<li key={key} className="todo-comp todo-item">{item}</li>
)
})}
</ul>
)
}
}
ReactDOM.render(
<TodoApp />,
document.getElementById('app'),
console.log("App has been rendered"))
【问题讨论】:
标签: javascript reactjs