【发布时间】:2020-12-20 20:07:53
【问题描述】:
我是 react 和前端应用程序的初学者,所以我决定向this youtube react tutorial 学习。我对html、js和css有一些了解。
链接导致我遇到问题的课程(在 React 中练习 state)。问题是:
onChange 分配给复选框的函数,当通过npm start 启动应用程序时,正在执行... 奇怪。如果我这样做,相同的功能会按预期工作:npm run build,serve -s build。
在教程中一切正常,我检查了我的代码,它与教程中的 100% 相同。我还将我的源代码发送给了我的一位同事,并且......一切都按照教程进行(我们都安装了 Windows 10)。
当尝试通过serve -s build 启动应用程序时,我鼓励我在this question 的帮助下解决了另一个问题(此系统上禁用了脚本的执行 - 我已经运行了Set-ExecutionPolicy RemoteSigned在 windows powerShell 中)。
我也尝试过删除 node_modules 并运行 npm install。
App.js(我已经删除了 App.js 的导入和导出):
class App extends React.Component {
constructor() {
super()
this.state = {
todos: checklist
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(id) {
console.log("debug1", id)
this.setState(prevState => {
const updatedToDos = prevState.todos.map(todo => {
if (todo.id === id) {
console.log(todo.id, id, todo.isChecked)
todo.isChecked = !todo.isChecked
console.log(todo.id, id, todo.isChecked)
}
console.log(todo)
return todo
})
console.log(updatedToDos)
return {
todos: updatedToDos
}
})
}
render() {
const todoChecklist = this.state.todos.map(item => <ToDoItm key={item.id} item={item}
handleChange={this.handleChange.bind(this)}/>)
return(
<div>
{todoChecklist}
</div>
)
}
}
checklist.js 是包含 TodoItems
集合的 json 文件ToDoItm(也删除了导出和导入):
function ToDoItm(props) {
return (
<div className="todo-item">
<h1>{props.item.line}</h1>
<input
type="checkbox"
checked={props.item.isChecked}
onChange={() => props.handleChange(props.item.id)}
/>
</div>
)
}
【问题讨论】:
标签: javascript node.js reactjs npm-start