Jump to content
Search Community

Jump into sections with ScrollTrigger in React

darky98 test
Moderator Tag

Go to solution Solved by OSUblake,

Recommended Posts

Hi, i tried to make this cool effect with React  and this is what i did: https://codesandbox.io/s/lingering-rain-03x71q?file=/src/components/Navbar.js The thing is that i want to jump into a section when i click any navbar link but as far as i know i cant use any states and when i use refs i get undefined.

I tried to copy and paste slideIn function into any onClick navlinks but it didint work, it breaks the whole app.

How can i achive this? Hope it was understood. Thanks

See the Pen PoWapLP by BrianCross (@BrianCross) on CodePen

Link to comment
Share on other sites

  • Solution

Hi darky,

 

Here's a simpler version of that demo.

 

See the Pen WNXXpvm by GreenSock (@GreenSock) on CodePen

 

I'm guessing to make it work you will need to make the gotoSection function available to outside the effect, like with a ref.

gotoSection.current = gotoSection;

 

And then listen for changes in an effect.

useEffect(() => {
  gotoSection.current(...)
}, [someProp])

 

Link to comment
Share on other sites

On 2/23/2022 at 2:31 PM, OSUblake said:

Hi darky,

 

Here's a simpler version of that demo.

 

 

 

 

I'm guessing to make it work you will need to make the gotoSection function available to outside the effect, like with a ref.

gotoSection.current = gotoSection;

 

And then listen for changes in an effect.

useEffect(() => {
  gotoSection.current(...)
}, [someProp])

 

Thank you so much, you saved my life haha, hope you have a good day :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...