我想用handleClick()函数实现3件事情。将buttonText切换为跟随或跟随,切换'active'类并处理FOLLOW动作。我可能做得不对。出于某种原因,onClick事件对此没有任何影响。有任何想法吗?由于处理ReactJs中的状态变化
class FollowButton extends React.Component {
constructor() {
super();
this.state = {};
this.state.following_state = true;
}
handleClick(event) {
event.preventDefault();
this.setState({following_state: !this.state.following_state});
let follow_state = following_state;
ProfilesDispatcher.dispatch({
action: FOLLOW,
follow_status: {
following: follow_state
}
});
}
render() {
let buttonText = this.state.following_state? "following" : "follow";
let activeState = this.state.following_state? 'active': '';
return (
<button className={classnames(this.props.styles, this.props.activeState)}
onClick={this.handleClick.bind(this)}>{buttonText}</button>
);
}
}
您是否在控制台中看到错误?我认为你需要 onClick = {this.handleClick} –
你应该在控制台中看到refrences错误,因为'following_state'没有在任何地方声明。我假设你想'var follow_state = this.state.following_state;'。 –
不,我没有在控制台中看到任何错误。那是奇怪的事情。 React 0.14需要点击事件才能使用bind() – hilarl