39

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;

3 Answers 3

44

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} />
Sign up to request clarification or add additional context in comments.

7 Comments

Buy why i have to do this? i dont need onChange handler, what i need to write inside this handler?
You do need it because React expects one. It's whats known as controlled or uncontrolled inputs. Read here why, facebook.github.io/react/docs/forms.html
I already tested this method but it does not work for my code. I am using the Checkbox component from Material UI.
@CasperLI Without any code to show it's impossible for anyone to help you debug. Either create a JSbin or JSfiddle to show your erroneous usecase or post a new question :)
You don't need an onChange handler. Not providing one wouldn't make the field "read only".
|
9

Just for others coming here. React now provides defaultChecked:

<label htmlFor={id}>
  <input
    id={id}
    type="checkbox"
    defaultChecked={input.props.checked}
    // disabled={input.props.disabled}
  />
  <span className="custom-checkbox"></span>
  {restChildren}
</label>

1 Comment

Changing "checked" to "defaultChecked" resolved the problem for me. Thanks !
8

This is one of the top hits on Google for "React Component not updating", so although the answer has been well accepted, I think it could be misleading.

The checkbox type doesn't require the onChange. I am not sure if this has changed since the answer was posted (current version of React is v15 - 2016-04-20).

See the following example of it working without an onChange handler (see JSBIN):

class Checky extends React.Component {
    render() {
      return (<input type="checkbox" checked={this.props.checked} />);
    }
}

class App extends React.Component {

    constructor(props) {
        super(props);
        this.state = { checked: true };
    }

    render() {
        return (
          <div>
            <a href="#" onClick={ () => { this.setState({ checked: !this.state.checked }); }}>Toggle</a>
            <hr />
            <Checky checked={this.state.checked} />
          </div>
        );
    }
}


React.render(<App />, document.body);

Another side note - a lot of answers (for similar questions) simply recommend "defaultChecked" - although this works, it is usually a half measure.

2 Comments

The above answer indicates that without the "OnChanged" handler, the checkbox will be rendered read-only. When I click your JSBin, that's the exact behavior I see: when I click the checkbox itself (not the link), nothing happens to the checked state. Is this your expectation?
Yes. My point is that "onChange" isn't required to make it an editable (non-read-only) component. How you trigger that state change (either via the checkbox onClick / onChange is up to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.