admin管理员组文章数量:1332377
I'm building a "todo" app for practicing & learning React basics. I have an array of objects from a file called TodosStores.js
which I passed down as props
to the TodoList
ponent like so:
import TodoList from './ponents/TodoList';
import TodosStores from './stores/TodosStore';
function App() {
return (
<div className="App">
<TodoList todos={TodosStores} />
</div>
);
}
export default App;
Now, I'm trying trying to toggle the property value of the current todopleted
to true
& false
so I can remove/add the todo-item-pleted
class. However, I'm getting this error: TypeError: Cannot create property 'pleted' on boolean 'true'
. I am seeking some explanation of what I'm doing wrong here. I e from Vue background so probably there's another way of changing the state
in React.
This is my ponent's code snippet:
import React, { Component } from 'react';
import './TodoListStyles.css'
class TodoList extends Component {
constructor(props){
super(props);
this.state = { todos: this.props.todos }
this.handleChange = this.handleChange.bind(this);
}
handleChange(item) {
return itempleted = !itempleted
}
render() {
return (
<ul className='todo-list'>
{
this.props.todos.map(
todo =>
<React.Fragment key={todo.id}>
<li className={`todo-item ${todopleted ? 'todo-item-pleted' : ''}`}>{ todo.title }</li>
<input type='checkbox' onChange={this.handleChange(todopleted)} checked={todopleted} />
</React.Fragment>
)
}
</ul>
)
}
}
export default TodoList;
PS: I'm using React Class Components because I want to contribute to an open-source project which uses class ponents. I might try refactoring later to hooks.
I'm building a "todo" app for practicing & learning React basics. I have an array of objects from a file called TodosStores.js
which I passed down as props
to the TodoList
ponent like so:
import TodoList from './ponents/TodoList';
import TodosStores from './stores/TodosStore';
function App() {
return (
<div className="App">
<TodoList todos={TodosStores} />
</div>
);
}
export default App;
Now, I'm trying trying to toggle the property value of the current todo.pleted
to true
& false
so I can remove/add the todo-item-pleted
class. However, I'm getting this error: TypeError: Cannot create property 'pleted' on boolean 'true'
. I am seeking some explanation of what I'm doing wrong here. I e from Vue background so probably there's another way of changing the state
in React.
This is my ponent's code snippet:
import React, { Component } from 'react';
import './TodoListStyles.css'
class TodoList extends Component {
constructor(props){
super(props);
this.state = { todos: this.props.todos }
this.handleChange = this.handleChange.bind(this);
}
handleChange(item) {
return item.pleted = !item.pleted
}
render() {
return (
<ul className='todo-list'>
{
this.props.todos.map(
todo =>
<React.Fragment key={todo.id}>
<li className={`todo-item ${todo.pleted ? 'todo-item-pleted' : ''}`}>{ todo.title }</li>
<input type='checkbox' onChange={this.handleChange(todo.pleted)} checked={todo.pleted} />
</React.Fragment>
)
}
</ul>
)
}
}
export default TodoList;
PS: I'm using React Class Components because I want to contribute to an open-source project which uses class ponents. I might try refactoring later to hooks.
Share Improve this question edited Apr 8, 2021 at 21:03 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Apr 3, 2021 at 20:01 Manuel AbascalManuel Abascal 6,3607 gold badges45 silver badges77 bronze badges 01 Answer
Reset to default 3The reason you are getting error TypeError: Cannot create property 'pleted' on boolean 'true'
is because you are passing boolean value to handleChange
in onChange={this.handleChange(todo.pleted)}
and in the function you are creating/mutating item.pleted
.
It can not create property pleted
on true
or false
basically.
本文标签:
版权声明:本文标题:javascript - I get this error in React - TypeError: Cannot create property 'completed' on boolean 'true& 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742306128a2449954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论