我正在學習ReactJS。我編寫了一個Web應用程序,該應用程序應該顯示一個輸入文本和一個按鈕「確認」,當按下它時,會顯示一個帶有「返回」按鈕的第二個輸入文本,以刪除新輸入。當第二個顯示時,第一個輸入和第一個按鈕被禁用。如何正確禁用ReactJS上的按鈕?
但是,我現在的劇本是不能接受的。我在將函數傳遞給函數FormRender的時候遇到了問題,並且在FormRender對象本身中指出了屬性「disabled」(它聲稱已經找到了「unexpected token」)。
有沒有更好的方法?
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>
);
}
});
你可以用'禁用= {真/假}',不需要串轉換。 [實施例](http://stackoverflow.com/a/41488965/6941627)。 –
@Roc你的代碼中有一些錯誤。 1.功能部件還沒有得到這樣的狀態: '{this.state.value}'將無法工作 2.在FormRender你得到的第一個參數獲得的道具,所以不要使用'this.props.disable '但是'props.disable' 3.當你指定像'onClick'這樣的事件處理程序時,你應該指定一個函數:'onClick = {this.props.Click}'(不包括括號) 4.當你定義道具時, :'作爲分配操作員。在大多數地方使用'=',但在少數地方使用':'。你必須改變它。 ,也許你應該使用'點擊= {this.In.bind(這)}' – Grajek
我忘記你的「意外的標記」錯誤。在FormRender中使用'disbale = {this.props.disable}'。 – Grajek