我有以下代码:
索引.js
class App extends React.Component {
constructor() {
super();
this.state = {
homeLink: "Home"
};
}
onGreet() {
alert("Hello!");
}
onChangeLinkName(newName) {
this.setState({
homeLink: newName
});
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-xs-10 col-xs-offset-1">
<Header homeLink={this.state.homeLink}/>
</div>
</div>
<div className="row">
<div className="col-xs-10 col-xs-offset-1">
<Home
name={"Max"}
initialAge={27}
greet={this.onGreet}
changeLink={this.onChangeLinkName.bind(this)}
initialLinkName={this.state.homeLink}
/>
</div>
</div>
</div>
);
}
}
和 首页.js
export class Home extends React.Component {
constructor(props) {
super();
this.state = {
age: props.initialAge,
status: 0,
homeLink: props.initialLinkName
};
}
onMakeOlder() {
this.setState({
age: this.state.age + 3
});
}
onChangeLink() {
this.props.changeLink(this.state.homeLink);
}
onHandleChange(event) {
this.setState({
homeLink: event.target.value
});
}
render() {
return (
<div>
<p>In a new Component!</p>
<p>Your name is {this.props.name}, your age is {this.state.age}</p>
<p>Status: {this.state.status}</p>
<hr/>
<button onClick={() => this.onMakeOlder()} className="btn btn-primary">Make me older!</button>
<hr/>
<button onClick={this.props.greet} className="btn btn-primary">Greet</button>
<hr/>
<input type="text" value ={this.state.homeLink}
onChange={(event) => this.onHandleChange(event)}/>
<button onClick={this.onChangeLink.bind(this)} className="btn btn-primary">Change Header Link</button>
</div>
);
}
}
一旦我在input
字段中写了一些东西并更新到state
,就会触发input
标签中的onChange
吗?当我在input
字段中编写内容时,我看不到Chrome中React Developer Tool
扩展程序中的state
变化。
当我单击按钮this.onChangeLink
它会触发onChangeLink
功能。onChangeLink
似乎没有任何参数,因为制动器是空的,我仍然可以在onChangeLink
函数内传递this.state.homeLink
this.props.changeLink
。this.props.changeLink
这也是index.js
中的一个函数,它接受一个参数newName
。我想这就是bind(this)
的用武之地。bind(this)
做什么?我可以用像(event) => this.onChangeLink(event)
这样的胖箭头函数重写它吗?
bind
是Function.prototype
中的一个函数,它返回绑定到当前this
的函数对象。
onClick={this.onChangeLink.bind(this)}
在这里onClick
函数将作为具有当前上下文的处理程序传递。
箭头函数根本不会创建新的this
上下文。this
将引用定义函数的上下文。
箭头函数不会创建自己的 this 上下文,因此这在封闭上下文中具有其原始含义。
onChange={(event) => this.onChangeLink(event)}
在这里,即使onChangeLink
未绑定,也会在同一this
上下文中的箭头函数中调用它。
所以..是的,你可以用一个胖箭头符号代替它以获得相同的效果。尽管在这种情况下,您必须重写参数列表两次。
您不需要onChangeLink
Home
组件。您可以直接将值传递给组件App
。
class Home extends React.Component {
constructor(props) {
super();
this.state = {
age: props.initialAge,
status: 0,
homeLink: props.initialLinkName
};
}
onMakeOlder() {
this.setState({
age: this.state.age + 3
});
}
onHandleChange(event) {
this.setState({
homeLink: event.target.value
});
}
render() {
return (
<div>
<input type="text" value ={this.state.homeLink}
onChange={(event) => this.onHandleChange(event)}/>
<button onClick={()=>this.props.changeLink(this.state.homeLink)} className="btn btn-primary">Change Header Link</button>
</div>
);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
homeLink: "Home"
};
}
onGreet() {
alert("Hello!");
}
onChangeLinkName(newName) {
this.setState({
homeLink: newName
});
}
render() {
console.log(this.state)
return (
<div className="container">
<div className="row">
<div className="col-xs-10 col-xs-offset-1">
</div>
</div>
<div className="row">
<div className="col-xs-10 col-xs-offset-1">
<Home
name={"Max"}
initialAge={27}
greet={this.onGreet}
changeLink={this.onChangeLinkName.bind(this)}
initialLinkName={this.state.homeLink}
/>
</div>
</div>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>