Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js can't change checkbox state

I created this simple TODO list, and when I want to check the checkbox I can't.

import React from 'react';  const TodoItem = React.createClass({    render() {     return (       <div>          <span>{this.props.todo}</span>         <input type="checkbox" checked={this.props.done} />        </div>     );   }  });  export default TodoItem; 

The parent:

import React from 'react'; import TodoItem from './TodoItem'; import AddTodo from './AddTodo';  const TodoList = React.createClass({    getInitialState() {     return {       todos: [{         todo: 'some text',         done:false       },{         todo: 'some text2',         done:false       },       {         todo: 'some text3',         done:true       }]     };   },     addTodo (childComponent) {     var todoText = childComponent.refs.todoText.getDOMNode().value;     var todo = {       todo: todoText,       done:false     };     var todos = this.state.todos.concat([todo]);     this.setState({       todos:todos     });      childComponent.refs.todoText.getDOMNode().value = '';   },    render() {      var Todos = this.state.todos.map(function(todo) {       return (            <TodoItem todo={todo.todo} done={todo.done} />         )     });      return (       <div>        {Todos}        <AddTodo addTodo={this.addTodo}/>       </div>      );   }  });  export default TodoList; 
like image 908
Bazinga Avatar asked Feb 12 '15 13:02

Bazinga


People also ask

How do I change the checkbox of a state in React JS?

Using setState with React Checkbox onChange Toggle the text of a paragraph with the checkbox using the 'useState' hook. */ import React, {useState} from 'react'; function Checkbox() { const [checked, setChecked] = useState(false); const handleChange = () => { setChecked(! checked); }; return ( <div> <p> {checked ?

How do you check and uncheck a checkbox in Reactjs?

To uncheck a checkbox programmatically in React, we can set the checked prop of the checkbox to a state. We have the checked state that we used to set the checked prop of the checkbox. Then we add a button that calls setChecked to toggle the checked value when we click the button.

How do you check if checkbox is checked React?

Use the target. checked property on the event object to check if a checkbox is checked in React, e.g. if (event. target. checked) {} .

How do I make a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!


1 Answers

When you haven't specified an onChange handler on your inputs React will render the input field as read-only.

getInitialState() {     return {         done: false     }; } 

and

<input type="checkbox" checked={this.state.done || this.props.done } onChange={this.onChange} /> 
like image 164
Henrik Andersson Avatar answered Sep 26 '22 21:09

Henrik Andersson