I have a react application with a good amount of states. I've been looking around trying to find the best way to save the states to local storage. Honestly, none of the answers were helping so I decided to ask my own.
What's the best way to go about saving these states to local storage?
function Main() {
const [thumb, setThumb] = useState();
const [timestamp, setTimestamp] = useState("1:20");
const [channelPic, setChannelPic] = useState();
const [title, setTitle] = useState("Title");
const [channelName, setChannelName] = useState("Channel");
const [views, setViews] = useState("1,000");
const [timeAgo, setTimeAgo] = useState("23");
const [increment, setIncrement] = useState("minute");
const [verified, setVerified] = useState(false);
return (
<div className="d-flex">
<Preview
thumb={thumb}
timestamp={timestamp}
channelPic={channelPic}
title={title}
channelName={channelName}
views={views}
timeAgo={timeAgo}
increment={increment}
verified={verified}
/>
<Info
setThumb={setThumb}
setTimestamp={setTimestamp}
setChannelPic={setChannelPic}
setTitle={setTitle}
setChannelName={setChannelName}
setViews={setViews}
setTimeAgo={setTimeAgo}
setIncrement={setIncrement}
setVerified={setVerified}
/>
</div>
);
}
export default Main;
EDIT: Since I'm rendering the states in <Preview />
but setting the states in <Info />
where am (or should I) be saving to local storage.
Is there a way to save all these states at the same time every time one of them is set or is that not how saving states to local storage works. Also where and when exactly should I add them to local storage, as in, where does the function to add them go? Whenever the state is changed? Or at certain increments?