如何在 reactjs 中使用钩子将 class 组件转换为功能组件
how to convert class component to function component with hooks in reactjs
我在 Class 组件中的代码
我对学习 React 还是个新手,我很难将 class 组件转换为功能组件。
class App extends React.Component {
handleChange = (e) => {
const { name, value } = e.target
this.setState({
[name]: value
})
}
configureCaptcha = () => {
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
'size': 'invisible',
'callback': (response) => {
this.onSignInSubmit();
console.log("Recaptca varified")
},
defaultCountry: "IN"
});
}
onSignInSubmit = (e) => {
e.preventDefault()
this.configureCaptcha()
const phoneNumber = "+91" + this.state.mobile
console.log(phoneNumber)
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
console.log("OTP has been sent")
}).catch((error) => {
console.log("SMS not sent")
});
}
render() {
.....
)
}
}
export default App;
我在 class 组件中的输出
如何在 reactjs 中将 class 组件转换为带钩子的函数组件
使用新语法的代码看起来像这样。但我建议将 name
和 mobile
的值严格分开存储 useStates
以使代码更清晰:
const App = () => {
const [values, setValues] = useState({});
const handleChange = (e) => {
const { name, value } = e.target;
setValues({ [name]: value, ...values });
};
const configureCaptcha = () => {
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
"sign-in-button",
{
size: "invisible",
callback: (response) => {
onSignInSubmit();
console.log("Recaptca varified");
},
defaultCountry: "IN",
}
);
};
const onSignInSubmit = (e) => {
e.preventDefault();
configureCaptcha();
const phoneNumber = "+91" + values.mobile;
console.log(phoneNumber);
const appVerifier = window.recaptchaVerifier;
firebase
.auth()
.signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
console.log("OTP has been sent");
})
.catch((error) => {
console.log("SMS not sent");
});
};
return <div></div>;
};
export default App;
我在 Class 组件中的代码
我对学习 React 还是个新手,我很难将 class 组件转换为功能组件。
class App extends React.Component {
handleChange = (e) => {
const { name, value } = e.target
this.setState({
[name]: value
})
}
configureCaptcha = () => {
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
'size': 'invisible',
'callback': (response) => {
this.onSignInSubmit();
console.log("Recaptca varified")
},
defaultCountry: "IN"
});
}
onSignInSubmit = (e) => {
e.preventDefault()
this.configureCaptcha()
const phoneNumber = "+91" + this.state.mobile
console.log(phoneNumber)
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
console.log("OTP has been sent")
}).catch((error) => {
console.log("SMS not sent")
});
}
render() {
.....
)
}
}
export default App;
我在 class 组件中的输出
如何在 reactjs 中将 class 组件转换为带钩子的函数组件
使用新语法的代码看起来像这样。但我建议将 name
和 mobile
的值严格分开存储 useStates
以使代码更清晰:
const App = () => {
const [values, setValues] = useState({});
const handleChange = (e) => {
const { name, value } = e.target;
setValues({ [name]: value, ...values });
};
const configureCaptcha = () => {
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
"sign-in-button",
{
size: "invisible",
callback: (response) => {
onSignInSubmit();
console.log("Recaptca varified");
},
defaultCountry: "IN",
}
);
};
const onSignInSubmit = (e) => {
e.preventDefault();
configureCaptcha();
const phoneNumber = "+91" + values.mobile;
console.log(phoneNumber);
const appVerifier = window.recaptchaVerifier;
firebase
.auth()
.signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
console.log("OTP has been sent");
})
.catch((error) => {
console.log("SMS not sent");
});
};
return <div></div>;
};
export default App;