0
output look like this當我單擊更改文本按鈕時,它應該用該值替換文本(TodoApplication),如果它是空值(應該替換空標題),則此代碼中的值不會更改!任何人都可以編輯代碼來獲得解決方案!更改onclick在react-js中的值
var TodoApp = React.createClass({
getInitialState: function() {
return {items: []};
},
updateItems: function(newItem) {
var allItems = this.state.items.concat([newItem]);
this.setState({items: allItems});
},
render: function() {
return (
<div>
<TodoBanner/>
<TodoList items={this.state.items}/>
<TodoForm onFormSubmit={this.updateItems}/>
</div>
);
}
});
var TodoBanner = React.createClass({
render: function() {
return (
<div className="container">
<h3>TODO Application</h3>
</div>
);
}
});
var TodoList = React.createClass({
render: function() {
var createItem = function(itemText) {
return (
<TodoListItem>{itemText}
</TodoListItem>
);
};
return <ul>{this.props.items.map(createItem)}</ul>;
}
});
var TodoListItem = React.createClass({
render: function() {
return (
<li>{this.props.children}</li>
);
}
});
var TodoForm = React.createClass({
getInitialState: function() {
return {item: ''};
},
handleSubmit: function(e) {
e.preventDefault();
this.props.onFormSubmit(this.state.item);
this.setState({item: ''});
React.findDOMNode(this.refs.item).focus();
return;
},
onChange: function(e) {
this.setState({item: e.target.value});
},
render: function() {
return (
<div className="container">
<form onSubmit={this.handleSubmit}>
<input type='text' ref='item' onChange={this.onChange} value={this.state.item}/>
<input type='submit' value='Add' className="btn btn-success"/><br/>
<input type='text' ref='item' onChange={this.onChange} value={this.state.item}/>
<input type='submit' value='Change Text' className="btn btn-success"/>
</form>
</div>
);
}
});
ReactDOM.render(
<TodoApp/>, document.getElementById('todo'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script>
<div id="todo" />
請正確解釋您的問題。 –
好吧!其實我想改變todobanner函數()中的文本與第二個輸入框的值作爲從用戶點擊第二個按鈕在渲染函數()幫助我!@RIYAJKHAN – gkr
@gkraj看到我的答案 –