我面临一些情况onClick
或onSubmit
功能需要event
和parameter
。我可以在 react 或 vanilla html 标签中使用它吗?
或者,只是使用其他功能做一些副作用?
const CustomFC = ({}) => {
const [ id, setId] = useState("");
const [password, setPassword] = useState("");
type FormParams = {
id: string;
password: string;
}
const customClickHandler = useCallback((params: FormParams) =>
{
dispatch(params);
}, [id, password]);
const onChangeId = useCallback((id) => setId(id),[]);
const onChangePassword = useCallback((password) => setPassword(password),[]);
<Form
onClick ={customClickHandler}
onChange={{onChangeId, onChangePassword}}
/>
}
// Form.tsx
const Form = ({
onSubmit,
onChange,
}) => {
const { onChangeId, onChangePassword } = onChange;
const handleSubmit = ( e, params ) => { // <- can this be possible?
e.preventDefault();
onSubmit(params);
}
return (
<form onSubmit={handleSubmit}>
<input ... />
<input ... />
</form>
)
}