Discovery Tech (1)
· One min read
React.useState(() => defaultValue)
- Some open-source SaaS (Open source is not free)
React
useState(initialValue)
can put callback for setting default value
- Normal
- Callback
const Modal = ({show, onHide, children, ...rest}) => {
const [config, setConfig] = useState({});
useEffect(() => {
const newConfig = buildConfig(rest);
setConfig(newConfig);
}, []);
return (
<Modal
show={show}
onHide={onHide}
{...config}>
{children}
</Modal>
);
}
const Modal = ({show, onHide, children, ...rest}) => {
const [config, setConfig] = useState(() => buildConfig(rest));
// Yeap, it's done!
return (
<Modal
show={show}
onHide={onHide}
{...config}>
{children}
</Modal>
);
}