0

I have a problem with routing to a specific part of another functional component in React. I have declared Route in Main component and Link around the place where I want to redirect from... it looks like this... The main function gets:

<Switch>
    <Route exact path='/news/:eyof' component={News} />
    <Route exact path='/news/:svetsko' component={News} />
    <Route exact path='/news' component={News} />
</Switch>

And I defined in other functional component:

<div className="card">
  <Link to="/news/:svetsko">
    <div className="card-header">
      <h3>Artistic gymnastics world championship</h3>
     </div>
   </Link>
</div>

So by clicking on this link, I need to get redirected to News page class "svetsko" so I can read about that news... All news are in containers in same page....

9
  • Have you tried replacing the News component with the Svetsko component on the route? Commented Jan 16, 2020 at 13:50
  • Svetsko isn't component, it is className inside News component... Commented Jan 16, 2020 at 13:51
  • In News component you must read parameters from router and then make decision what to do with that parameter (load content for that apply different style or soemthing else) Commented Jan 16, 2020 at 13:52
  • what is main problem you are getting??? Commented Jan 16, 2020 at 13:53
  • 1
    If you use hooks you can use this link (check how he used useParams) reacttraining.com/react-router/web/example/url-params Commented Jan 16, 2020 at 13:54

3 Answers 3

1

Try something like this in your component:

let { scrollToSection } = useParams();
svetskoRef = React.createRef();

useParams() allows you to access :svetsko. When the component loads, you can use scrollToSection to navigate between different parts of the page. The scrollRef is used to access the DOM element you want to scroll to.

window.scrollTo(0,scrollRef.offsetTop)

The markup would look something like this:

<div className="card" ref="svetskoRef">
  <Link to="/news/:svetsko">
    <div className="card-header">
      <h3>Artistic gymnastics world championship</h3>
     </div>
   </Link>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

Should I declare it in News component since I'm trying to access the className in it?
Yep, this is where you will grab the route param and use the ref.
import { useParams, scrollRef } from 'react-router-dom' const News = () => { let { scrollToSection } = useParams(); scrollRef = React.createRef(); window.scrollTo(0,scrollRef.offsetTop) return ( trows error saying scrollRef isn't defined...
add ref="scrollRef" to the svetsko element in your markup
1

You need only one route like

<Switch>
    <Route exact path='/news' component={News} />
</Switch>

you can give link like

 <Link to={{
  pathname: '/news',
  state: { news : yourNewsName } // you can pass svetsko here
}}> 

 <div className="card-header">
  <h3>Artistic gymnastics world championship</h3>
 </div>

</Link>

You can access this state in your News Page like

<div>{  props.location.state !== undefined ? props.location.state.news : '' }</div>

After getting your news type like eyof :

Step 1 :

   you can create on functional component which takes argument as your
 data and return your new post jsx with your data.

Step2 :

   So,when you get your eyof in your page then you are going to call 
this function and pass your data related to your eyof and that function 
return your new post to your page.

4 Comments

this is actually taking me to news page, but not on directed path... ` <div className="card"> <Link to={{ pathname : "/news", state: { news: 'eyof'} }}> <div className="card-header"> <h3>EYOF 2019. year</h3> </div> </Link> </div> ` and I puted last line of code inside od targeted div... shuld I wrap it around text or what? But any way, state isn't commited...
no, look like you are trying to pass your child with link don't do that you have to render your component conditionally in your news page
So it would be better for me to declare component called eyof, svetsko, and rest, so I can call them true conditional declaration and render it?
and what do you think about putting all className-s content as a JSON file with id: eyof, content: text I'm using? It can save some performance and speed webpage up...
1

Ok, I found one really good library as a solution, it's called react-scroll and it has an option to wrap link you need in earlier defined scroll link, and to wrap component you want it to link as a specific part of the page with Element with id you gave to scroll link as a parameter... Try it if you need it somehow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.