我试图将点击处理程序传递给 SignIn 组件,但它对我不起作用。我得到日志然后页面刷新
Auth
class:
class Auth extends Component {
login() {
console.log('Clicked'); //only this method works
fetch('/api/auth/signin', {
method: 'POST',
body: JSON.stringify(this.state),
headers: {
'Content-Type': 'application/json; charset=utf8'
}
}).then((response) => {
if (response.status === 200) {
this.props.history.push('/api/categories')
}
})
}
render() {
return (
<SignIn onCustomClick={this.login}/> //onClick handler
)
}
登录组件
export default function SignIn(props) {
const {onCustomClick} = props; // props
return (
<Button
type="submit"
fullWidth
variant="contained"
color="primary"
className={classes.submit}
onClick={onCustomClick} // onClick handler
>)
}