I'm beginner with react and making frontend applications, so I decide to learn with this youtube react tutorial. I have some knowlage about html, js, and css.
Link leads to lesson (exercise about state
in React) where I encounter a problem. The problem is:
onChange
function assigned to check box, when starting app by: npm start
, is acting... strange. Same function works as expected if I do: npm run build
, serve -s build
.
In tutorial everything works fine, i've checked my code and it's 100% same as in tutorial. I've also send my source code to one of my colleagues and ... everything works as in tutorial (we both have Windows 10 installed).
When trying to launch app by serve -s build
, i encourage another problem which i solved with help of this question (execution of scripts is disabled on this system - I've run Set-ExecutionPolicy RemoteSigned
in windows powerShell).
I've also tried deleting node_modules and run npm install
.
App.js (I've deleted imports and export of 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 is json file with collection of TodoItems
ToDoItm (also export and imports deleted):
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>
)
}
PS.
Acting strange i mean - this what happends when i click first checkBox: