Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 更改选择后,默认选中的不起作用_Reactjs - Fatal编程技术网

Reactjs 更改选择后,默认选中的不起作用

Reactjs 更改选择后,默认选中的不起作用,reactjs,Reactjs,我有三个单选按钮和一个选项是默认选中弹出窗口。 问题是,当我选择任何非默认选项时,关闭弹出窗口并再次打开,然后仍然选中以前选择的选项,但不是默认选项。 请建议如何在每个弹出窗口上保持选中默认选项 弹出组件 var PopupComponent=React.createClass({ getInitialState:函数(){ 返回{typed:'all'}; }, handleChange:函数(e){ this.setState({typed:e.target.value}); }, hand

我有三个单选按钮和一个选项是默认选中弹出窗口。 问题是,当我选择任何非默认选项时,关闭弹出窗口并再次打开,然后仍然选中以前选择的选项,但不是默认选项。 请建议如何在每个弹出窗口上保持选中默认选项

  • 弹出组件
  • var PopupComponent=React.createClass({
    getInitialState:函数(){
    返回{typed:'all'};
    },
    handleChange:函数(e){
    this.setState({typed:e.target.value});
    },
    handleClick:函数(e){
    //按钮点击处理程序逻辑
    $(“#警报模式”).modal('hide');//隐藏弹出窗口
    $('.modal container')[0].style.display=“无”;
    },
    render:function(){
    var options=this.props.options;
    var defaultOptIndex=this.props.checkedIndex;
    var放射性项=[];
    
    对于(var i=0;i由于您刚刚关闭handleClick()中的弹出窗口,您可以使用setState()将状态更新为handleClick()中的默认值;

    这是有意义的,但不确定我必须使用setState()在handleClick()中设置什么才能设置选中的默认选项;
    var PopupComponent = React.createClass({
        getInitialState: function() {
            return {typed: 'all'};
        },
    
        handleChange: function(e) {
            this.setState({typed: e.target.value});
        },
    
        handleClick: function(e) {
            // button click handler logic
    
            $("#alert-modal").modal('hide'); // hide popup
            $('.modal-container')[0].style.display = "none"; 
        },
    
        render: function() {
            var options = this.props.options;
            var defaultOptIndex = this.props.checkedIndex;
            var radioItems=[];
    
            for(var i=0;i<options.length;i++){
                radioItems.push(
                    <div key={i} className="modal__radio-group-item">
                        <label><input value={options[i].value} type="radio" name="radio" defaultChecked={defaultOptIndex == i} /> <span /> {options[i].string}</label>
                    </div>
                );
            }
    
            return(
                <div className="modal modal--active">
                <div className="modal__content" style={{ width: '100%', maxWidth: 540, display: 'flex', alignItems: 'center', flexDirection: 'column' }}>
                    <p className="modalTitle">{this.props.title}</p>
                    <div className="modal__radio-group" onChange={this.handleChange}>
                        {radioItems}
                    </div>
                    <div className="modal__buttons-container">
                        <button className="button" onClick={this.handleClick}>{R('continue_button')}</button>
                    </div>
                </div>
                </div>
            );
        }
    });
    
    function showPopup(header_title, options, checkedIndex, func, index) {
        ReactDOM.render(<PopupComponent title={header_title} options={options} checkedIndex={checkedIndex} callback={func} provider_index={index} />, document.getElementById('alert-modal'));
        $('.modal-container')[0].style.display = "flex";
        $("#alert-modal").modal('show');
    }