当我为材质ui RaisedButton上的onClick事件调用动作创建者时,出现以下错误
道具类型失败:提供给onClick
的类型为object
的道具RaisedButton
无效,预期为function
。
这是我写的组件
import React, { Component } from 'react';
import RaisedButton from 'material-ui/RaisedButton';
import { connect } from 'react-redux';
import * as actions from '../actions';
class App extends Component {
authButton = () => {
if(this.props.authenticated) {
return <RaisedButton label="Sign out" onClick={this.props.authenticate(false)} />
}
return <RaisedButton label="Sign out" onClick={this.props.authenticate(true)} />
}
render() {
return (
<div>{this.authButton()}</div>
);
}
}
const mapStateToProps = (state) => {
return { authenticated: state.authenticated };
}
export default connect(mapStateToProps, actions)(App);
如何摆脱这个错误并使之发挥作用?
最佳答案
它期望功能,所以尝试这个onClick={() => { this.props.authenticate(false)}}