const CloseAccount = React.memo(() => {
const submit = () => {
console.log('....')
}
const canSubmit = (event:any) => {
if(event.target.type === 'text') {
state.email = event.target.value
} else if (event.target.type === 'checkbox') {
state.checkState = event.target.checked
}
console.log(!(state.email && state.checkState))
}
const state = {
email: '',
checkState: false,
}
return (
<div className={styles.container}>
<section className={styles.email}>
<div className={styles.emailTips}>
输入可用邮箱,获取注销结果
</div>
<input
className={styles.emailInput}
placeholder="*请输入你的邮箱"
onChange={canSubmit}
defaultValue={state.email}
/>
</section>
<section className={styles.agree}>
<input type="checkbox"
className={styles.agreeCheck}
onChange={canSubmit}
/>
<label className={styles.agreeLabel}>我已阅读并同意<a>“账户注销须知”</a></label>
</section>
<section className={styles.submit}>
<button className={styles.submitButton} onClick={submit} disabled={!(state.email && state.checkState)}>
<span className={styles.submitWord}>申请注销</span>
</button>
</section>
</div>
)
})
CloseAccount.displayName = 'CloseAccount'
export default CloseAccount
逻辑是刚开始的时候button是disabled的,当用户输入了邮箱和勾选了同意后才可以点击按钮。我这样写button的disabled的值不会变,请问我的代码哪里需要改正
去看看hooks的useState方法可以触发组件更新,你这样写组件没法更新,所以不会生效。