React.js无法更改复选框状态

Baz*_*nga 34 javascript reactjs

我创建了这个简单的TODO列表,当我想检查复选框时我不能.

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;
Run Code Online (Sandbox Code Playgroud)

父母:

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;
Run Code Online (Sandbox Code Playgroud)

Hen*_*son 42

如果未onChange在输入中指定处理程序,则React会将输入字段呈现为只读.

getInitialState() {
    return {
        done: false
    };
}
Run Code Online (Sandbox Code Playgroud)

<input type="checkbox" checked={this.state.done || this.props.done } onChange={this.onChange} />
Run Code Online (Sandbox Code Playgroud)

  • 买我为什么要这样做?我不需要onChange处理程序,我需要在这个处理程序内写什么? (6认同)
  • 你确实需要它,因为React期待一个.它被称为受控或不受控制的输入.在这里阅读原因,http://facebook.github.io/react/docs/forms.html (5认同)

Chr*_*ris 7

这是谷歌"React Component not updating"的热门歌曲之一,所以尽管答案已被广泛接受,但我认为这可能会产生误导.

checkbox类型不需要onChange.自答案公布后我不确定这是否有所改变(当前版本的React是v15 - 2016-04-20).

请参阅以下示例,它没有onChange处理程序(请参阅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);
Run Code Online (Sandbox Code Playgroud)

另一个注意事项 - 很多答案(对于类似的问题)只是推荐"defaultChecked" - 虽然这有效,但通常是一半.


muu*_*uuv 6

只是为了其他人来这里。React 现在提供defaultChecked

<label htmlFor={id}>
  <input
    id={id}
    type="checkbox"
    defaultChecked={input.props.checked}
    // disabled={input.props.disabled}
  />
  <span className="custom-checkbox"></span>
  {restChildren}
</label>
Run Code Online (Sandbox Code Playgroud)