Skip to content
Advertisement

Sending data in draft by going in previous page or leaving current page

I am making a website like yahoo mailing system, there I need to make draft messages, I want as soon as a person writes in any field and when he leaves the page with out submitting that form this form gets saved in the array of drafts msgs, please guid me I am using react js for frontend

I did try using useeffect to do it by useeffect gets worked just by coming this page so it’s no use

Advertisement

Answer

Just useEffect is not enough, you will need to have also component state (useState). Then on every input change update the state and also the draft messages array (you probably need to make post request for this one). Something like this:

const [message, setMessage] = useState("");

useEffect(() => {
  //if there is draft message, get it and set it in state
}, []);

const onInputChange = (val) => {
  setMessage(val);
  //set your message to drafts and remove it from there as soon as it is submitted
}

<input onChange={(e) => onInputChange(e.target.value)} />
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement