It likely means that document.getElementById("mySidenav") is returning null and not the element.
But we need to see way more code than what you have posted to really be able to help. Do you have a GitHub with the code or some live version we can look at?
Edit: I have left the answer below but the other answers do the job with just a simple change, so you may not want to look at this, unless you are interested in exploring other approaches.
You can only play with the DOM - i.e. document.getElementById("mySidenav") - once those elements exist. And that is sometime after the render has occurred in React, as it needs to run the Render than figure out how to update the DOM, your code is run too early in the cycle.
There are at 2 solutions I can think of:
One is to use state to manage whether the nav is open or not, and then based on that state render:
I think what @lasjorg said is exactly the problem. In React (JavaScript), you only need to pass a pointer to function you want called upon an event. Except have done this: onClick={ e => closeNav(e) }…
Most of the point of React is to remove the need to do this. JSX is not HTML, it’s a syntax that lets you write functions that look like HTML (and those functions, when processed by the ReactDOM library, result in HTML that looks like the JSX).
To access that HTML using DOM methods you can jump through hoops like you’re trying to do. The second solution from @martinc shows we how to do this, but this is not a good reason to use refs, it complicates the code for no good reason (you’re deliberately fighting with React). It’s very unlikely that at this point you need to get into using refs at all: you can just adjust the style directly (as shown in the first solution), and tbh that’s how you should be writing this logic.
@DanCouper I totally agree, it is not the React way of doing things. I just wanted to point out what the problem was.
I would very much suggest that @oguzhanefe32 looks into the different ways of styling React components. There are not only better ways but much more powerful ways of styling components that let you use logic and component state for the styling.
Yes the problem with answering these sorts of questions is that there are multiple answers depending on what level you are aiming at.
At one level there is the quick fix : Use the arrow function to delay execution of the call until the event happens. It works! Job done!
But the OP will now get pain later when / if that element is shown/hidden based on state or props and the thing may or may not exist, or gets rendered again after the click or whatever.
So then it’s good to teach the best way, but you then have to go into why. I think your sentence “Most of the point of React is to remove the need to do this.” gets to the core of this, it’s sort of what I wanted to say.
I wonder why FCC has left someone thinking like this, I imagine it’s because you learn DOM manipulation first, you have your hammer that works nicely, then you learn React, and you are not told to put down the hammer as you are now using screws. You can hammer a screw in and it will work, but you might get problems later!
I would like to just make it clear that wrapping the handler inside an anonymous function is not required unless you need to pass some arguments other than the event to the handler. If all you need is the event, you do not need to wrap the handler.