Need help with exporting useref const.
in my app() function, i have all the content for the website imported as components.
And i want to modify the imported components via another component in HeaderNavigation using useRef however i cant seem to actually get the referenced element in my outside component, so i am guessing i am doing something wrong with exporting, or i have something wrong with useRef fundamentally.
App.js
NavMenu.js
This is the outside file that i want to import the ref's in : OmStaeck, Folkene, Karriere
16 Replies
Is it even possible to export/import useRef() variables?
What are you actually trying to accomplish with the useRef?
i am trying to use it to scroll smoothly to a section in app.js
HeaderNavigation is always visible
and i have segmented all of the code into components
so i need to export and import the useRef value
Ah got it. I'll try to see if I can get that working in React. I primarily just use html/css so this would be so simple to do with that, hopefully it isn't too hard in React
Yeah im new to learning react so im quite confused on how it works myself, thank you for your time, i appreciate it.
And yes that is what is annoying me, this would be so simple in normal html/css
Wait I just realized, why the hell am I or you doing this without a library? Have you looked into React Router?
I believe it's pretty easy with it
With React Router it's just:
yeah so im confused as well, because thats literally the solution i came up with when i first tried it, but my project-leader told me that i should use useRef() instead
I'm not a professional React dev so I could be missing something
And this would work across multiple components right?
It should yea
kk imma try it out
im trying to get it working with no library for fun rn but could take me a while i havent used react in a minute
if i even figure it out lmao
np lol
I got it
This is definitely not the way to go about it lol
vince1444
CodeSandbox
distracted-ptolemy-dypp26 - CodeSandbox
distracted-ptolemy-dypp26 by vince1444 using loader-utils, react, react-dom, react-scripts
What I did:
I think theoretically there should be a split second where the Navbar's anchor tag's href attribute defaults to just "/" but it is so fast with this application that it doesn't really mean anything