New To React Hook useState Is Returning Undefined New To React Hook useState Is Returning Undefined reactjs reactjs

New To React Hook useState Is Returning Undefined


To set initial state properly you should pass it to the useState as an argument, so:

const [notifications, setNotifications] = useState([]);

Also no matter if you set props.notifications outside somewhere or not, but if you rely on it having some kind of default value in the component, then you should set it right there, e.g.:

const Notifications = ({ notifications = [] }) => {

And the last but not the least, using array in dependency list of useEffect has some unwanted side-effects, for example if notifications structuruly will stay the same (same items, same length), but will be a new array, useEffect will miss a cache, since it only does a shallow comparison. Consider using a hashing function of some kind instead of the array itself (e.g. JSON.stringify)