const [credentials, setCredentials] = useState({name:"", password:""});
1 Replies
You are setting your initial state name and password as an empty string. when you refresh your page state is come back to the initial value which is n empty string. if you want to keep your state data after the page refresh, then you have to store that data in local storage.
import React, from "react";
const App=()=>{
const [count, setCount] = React.useState(1);
React.useEffect(() => { setCount(JSON.parse(window.localStorage.getItem('count'))); }, []);
React.useEffect(() => { window.localStorage.setItem('updated count', count); }, [count]);
const next = () => setCount(count+1);
const previous = () => setCount(count-1);
return (
<div>
<h1> Count {count} </h1>
<button onClick={next}>next</button>
<button onClick={previous}>previous</button>
</div>
);}
export Default App;