javascript - 通过继承 React.js 中的值来更改复选框的值

标签 javascript inheritance reactjs

我试图熟悉react.js 中组件之间的继承如何工作,但遇到了一些麻烦。

要求 - 基本上,我想要实现的是当我单击按钮时复选框的值发生变化。

复选框继承'checkedVal'的值,该值在标题中的状态中设置。

首次加载时,我可以在复选框中获取正确的值,但单击按钮时无法更改它。有什么想法吗?

var Heading = React.createClass({
propTypes: {
    name: React.PropTypes.string,
    age: React.PropTypes.number
},
getDefaultProps: function(){
    return {
        name: 'Keir',
        age: 24,
    }
},
getInitialState: function(){
    return {
        manU: false,
        checkedVal: false
    }
},
manUFan: function(){
    this.setState(function(previousState, currentProps){
        return {
            manU: !previousState.manU,
            checkedVal: !previousState.checkedVal
        }
    });
},
render: function(){
    var msg;
    if(this.state.manU){
        msg = "I am a United fan."   
    } else {
        msg = "I dream of being a united fan."   
    }
    return (
        <div>
            <h1>Attempting React</h1>
            <ul>
                <li>{this.props.name}</li>
                <li>{this.props.age}</li>
            </ul>
            <button onClick={this.manUFan}>Do You Support Man U?</button>
            <CheckBox checkBoxVal={this.state.checkedVal}/>
            <p>{msg}</p>
        </div>
        )
}
});

var CheckBox = React.createClass({
    propTypes: {
        checkBoxVal: React.PropTypes.bool  
    },
    render: function(){
        return (
            <div>
                <input type="checkbox" defaultChecked={this.props.checkBoxVal}/>
            </div>
            )
    }
    });


ReactDOM.render(<Heading />, document.getElementById('content'));

最佳答案

The defaultValue and defaultChecked props are only used during initial render. If you need to update the value in a subsequent render, you will need to use a controlled component. https://facebook.github.io/react/docs/forms.html#controlled-components

因此,根据您的情况,以下是实现受控 CheckBox 的方法。

var Heading = React.createClass({
...
    getInitialState: function(){
        return {
            checkedVal: false
        }
    },

    handleChange: function(event){
        this.setState({checkedVal: event.target.value});
    },

    render: function(){
        ...
        return (
            <div>
                ...
                <CheckBox onChange={this.handleChange} checkBoxVal={this.state.checkedVal}/>
                ...
            </div>
        );
    }
});

var CheckBox = React.createClass({
    propTypes: {
        checkBoxVal: React.PropTypes.bool  
    },

    render: function(){
        return (
            <div>
                <input type="checkbox" onChange={this.props.onChange} value={this.props.checkBoxVal}/>
            </div>
        );
    }
});

关于javascript - 通过继承 React.js 中的值来更改复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066861/

相关文章:

javascript - Kendo UI 颜色选择器不显示选择颜色的对话框

python - 属性是否支持继承?

C# : dynamic polymorphism with non polymorphic classes

c++ - 类的前向声明/不完整类型的无效使用

reactjs - 如何防止一个组件中的 css 影响 React 中另一个组件中的 css?

javascript - 当计数器有某个值时如何触发事件?

javascript - HTML - 将文本输入绑定(bind)到标签

javascript - 使用多部分文件和 json 数据响应 ajax 请求

javascript - react : changing state onMouseOver

reactjs - 搜索不适用于使用远程数据的 React Material 表