我想用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