is it possible to use a function
as my React Component's state ?
example code here:
// typescript
type OoopsFunction = () => void;
export function App() {
const [ooops, setOoops] = React.useState<OoopsFunction>(
() => console.log('default ooops')
);
return (
<div>
<div onClick={ ooops }>
Show Ooops
</div>
<div onClick={() => {
setOoops(() => console.log('other ooops'))
}}>
change oops
</div>
</div>
)
}
but it doesn't works ... the defaultOoops
will be invoked at very beginning, and when clicking change oops
, the otrher ooops
will be logged to console immediately not logging after clicking Show Ooops
again.
why ?
is it possible for me to use a function as my component's state ?
or else React has its special ways to process such the function state
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…