Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove whitespace from a pathname in react

The menu items in my dropdown are stored in an array called "lis". When the dropdown is open, the lis are mapped into it. Now I want to take the names not only as content, but also as a path to open the pages. And it works. My only problem is that some of the lis consist of two words. And I can’t have a pathname with spaces. How can I remove this whitespace?
I tried to predefine it with lis.join(""), but the console tells me, undefined, so I obviously must find a way to do that in the map function.

code:

{
 item.lis.map((li, index)=>(
    <ul key={index}>
     <DropDownLi><Link to={{pathname:`/${li}`}} className="link" style={{color:"black"}}>{li}</Link></DropDownLi>
    </ul>
     ))
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you could use replace to remove spaces, as str.replace(/\s+/g, '') which replaces spaces,

{
 item.lis.map((li, index)=>(
    <ul key={index}>
     <DropDownLi><Link to={{pathname:`/${li.replace(/\s+/g, '')}`}} className="link" style={{color:"black"}}>{li}</Link></DropDownLi>
    </ul>
 ))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading