简体   繁体   English

如何正确禁用ReactJS上的按钮?

[英]How to disable buttons on ReactJS properly?

I am learning ReactJS. 我正在学习ReactJS。 I wrote a web app that should show an input text and a button 'Confirm', and when pressed it shows a second input text with a button 'Go back' which removes the new input. 我编写了一个Web应用程序,该应用程序应显示输入文本和“确认”按钮,当按下该按钮时,它会显示第二个输入文本,并带有“返回”按钮,该按钮将删除新输入。 When the second one is showed, the first input and the first button are disabled. 当显示第二个时,第一个输入和第一个按钮被禁用。

However, my current script is not accepted. 但是,我当前的脚本不被接受。 I'm having problems with passing functions as prop to the function FormRender and with indicating in the FormRender object itself the property "disabled" (it claims to have found "unexpected token"). 我在将函数作为道具传递给函数FormRender以及在FormRender对象本身中指示属性“已禁用”(它声称已找到“意外令牌”)时遇到了问题。

Are there any better approaches? 有没有更好的方法?

function FormRender (props) {
    return (
        <div>
            <input type = "text" placeholder = {this.props.holder} {this.props.disable} />
            <button onClick = {this.props.Click()} {this.props.disable} > {this.state.value} </button>
        </div>
    );
};

var R  = React.createClass ({
    getInitialState: function () {
        return { names: [], disable: false }
    },
    update: function () {
        return this.state.disable ? "disabled" : "";
    },
    componentDidMount: function () {
        var a = this.state.names;
        a.push ( <FormRender holder = 'Name' value = 'Confirm' Click = {this.In}, disable: {this.update} /> );
        this.setState ( { names: a } );
        this.forceUpdate();
    },  
    In: function () {
        var a = this.state.names;
        this.setState ( { disable: true } );
        a.push ( <FormRender holder = 'Surname ' value = 'Back' Click = {this.Out} disable: "" /> );
        this.setState ( { names: a } );
        this.forceUpdate();
    },
    Out: function () {
        var a = this.state.names;
        this.setState ( { disable: false } );
        a.splice(a.length-1,1);
        this.setState ( { names: a } );
        this.forceUpdate();
    },  
    render: function () {
        return (
            <div>
                {this.state.names}
            </div>
        );
    }
});

I am not sure if it is what you want my example 我不确定这是否是您想要的示例

function FormRender (props) {
return (
    <div>
        <input
            type="text"
            placeholder={props.holder}
            disabled={props.disable()}
        />
        <button
            onClick={props.click}
            disabled={props.disable()}
        >
            {props.value}
        </button>
    </div>
);
};

var R  = React.createClass ({
getInitialState: function () {
    return {names: [], disable: false}
},

update: function () {
    return this.state.disable;
},

componentDidMount: function () {
    var a = this.state.names;
    var form = {
        holder: 'Name',
        value: 'Confirm',
        click: this.In,
        disable: this.update
    };

    a.push(form);
    this.setState({names: a});
},  
In: function () {
    if (this.state.names.length > 1) {
        return;
    }

    var a = this.state.names;
    var form = {
        holder: 'Surname',
        value: 'Back',
        click: this.Out,
        disable: function () {
            return false
        }
    }

    this.setState({disable: true});
    a.push(form);
    this.setState({names: a});
},
Out: function () {
    var a = this.state.names;
    this.setState({disable: false});
    a.splice(a.length-1,1);
    this.setState({names: a});
},  
render: function () {
    return (
        <div>
            {this.state.names.map(function (el) {
                return (<FormRender
                    holder={el.holder}
                    value={el.value}
                    click={el.click}
                    disable={el.disable}
                />);
            })}
        </div>
    );
}
});

https://jsfiddle.net/69z2wepo/72509/ https://jsfiddle.net/69z2wepo/72509/

You have got many syntax errors in you code. 您的代码中有很多语法错误。 I recommend use linter :) 我建议使用linter :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM