0
我有一个组件的父子关系,我正在尝试设置。我希望让父母保持对交易原始状态(React组件)的了解,然后根据是否小于原始金额确定是否允许添加新交易。点击通话时,我不知道如何正确地将数据从组件传递到组件。下面列出了我的原始组件。请发送帮助!反应 - 将变量传递给另一个组件
```
var transaction = React.createClass({
getInitialState: function(){
return {
transactions: [ { desc: '', val: 0 } ],
initialTransaction: 10
}
},
render: function(){
return (
<div>
{ this.state.transactions.map(this.renderChild) }
<button onClick={ this.newTransaction }>Add</button>
</div>
)
},
shouldComponentUpdate: function(nextProps, nextState) {
console.log(nextState.transactions);
return nextState.transactions.reduce(function(a, b) {
return { val: a.val + b.val}
}) == this.state.initialTransaction
},
newTransaction: function(_transaction,x,y){
this.state.transactions.push(_transaction);
this.setState({
transactions: this.state.transactions
});
this.forceUpdate();
},
renderChild: function(_transaction){
return (
<div>
<input type='text' defaultValue={ _transaction.desc } />
<input type='text' defaultValue={ _transaction.val }/>
// TODO: this button should pass a transaction up to the parent component
</div>
);
}
});
React.renderComponent(<transaction />, document.getElementById('mount-point'));
```
确定,所以可以说我不想在每个子元素上都有那个按钮。我想有一个'添加'按钮,向数组添加一个新的事务,然后让孩子重新呈现,以便从那里获取所有新元素。我会怎么做? – noname
另外,在renderChild调用中提到的'this'是什么? 'this'的道具元素是什么? – noname