3 回答

TA貢獻2012條經驗 獲得超12個贊
OP使這個問題成為一個移動的目標。它又被更新了。因此,我覺得有責任更新我的答復。
首先,回答您提供的示例:
onClick
this.props.onClick.bind(null, this)
:
var Child = React.createClass({ render: function () { return <a onClick={this.props.onClick.bind(null, this)}>Click me</a>; }});
onClick: function (component, event) { // console.log(component, event); },
但這個問題本身就有誤導性。
props
.
var Child = React.createClass({ render: function () { return <a onClick={this.props.onClick}> {this.props.text} </a>; }});var Parent = React.createClass({ getInitialState: function () { return { text: "Click here" }; }, onClick: function (event) { // event.component.props ?why is this not available? }, render: function() { return <Child onClick={this.onClick} text={this.state.text} />; }});
在這個例子中,你已經清楚地知道了什么是孩子的道具。
如果它真的是關于使用孩子的道具…
props
var Child = React.createClass({ render: function () { return <a {...this.props}> {this.props.text} </a>; }});
var Parent = React.createClass({ getInitialState: function () { return { text: "Click here" }; }, onClick: function (text) { alert(text); }, render: function() { return <Child onClick={this.onClick.bind(null, this.state.text)} text={this.state.text} />; }});
當您連接其他的子組件時,不需要額外的配置
var Parent = React.createClass({ getInitialState: function () { return { text: "Click here", text2: "No, Click here", }; }, onClick: function (text) { alert(text); }, render: function() { return <div> <Child onClick={this.onClick.bind(null, this.state.text)} text={this.state.text} /> <Child onClick={this.onClick.bind(null, this.state.text2)} text={this.state.text2} /> </div>; }});
一個健壯的實例
孩子們完全無知。
ServiceItem
<div onClick={this.props.handleClick.bind(this.props.index)} />
handleClick
index
[
父母是孩子
DTServicesCalculator
DTServiceCalculator
ServiceItem
ServiceItem
<ServiceItem chosen={chosen} index={i} key={id} price={price} name={name} onSelect={this.props.handleServiceItem} />
handleServiceItem
handleServiceClick (index) { this.props.onSelect(index);}
所有人都知道
handleSelect (index) { let services = […this.state.services]; services[index].chosen = (services[index].chosen) ? false : true; this.setState({ services: services });}
結語

TA貢獻1811條經驗 獲得超4個贊
var Child = React.createClass({ render: function() { <a onClick={this.props.onClick.bind(null, this)}>Click me</a> }});var Parent = React.createClass({ onClick: function(component, event) { component.props // #=> {Object...} }, render: function() { <Child onClick={this.onClick} /> }});
bind(null, this)
this.props.onClick
component
event
添加回答
舉報