Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React state variable value appearing to be null

I’m trying to access a value of my state variable, but it appears to be null when I change the date, but If I change any other input field, I don’t get it null
Here is the link to stackblitz running app.

If you open the console and type something in any field you can check the value of filter variable to be same as what you type, but if you click on the date range input, it will show you null, I’m scratching my head for hours around this.
Thanks in advance to helping hands

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

react-bootstrap-daterangepicker looks like abandonware, I’d pick a different library.

If you want to keep using it, note that the onEvent callback isn’t updated when your component re-renders. So to get the latest state, use the setState callback:

    const onDateChange = (event, picker) => {
        ...
        setFilter(filter => {
            console.log('new filter', filter);
            return {...filter, hi: 'bye'};
        });
    }

This will let you access the latest filter state of your component even though the callback has closed over the stale initial render state.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading