Santu G
Santu G

Reputation: 11

How to redirect to URL in react without reloading entire Page

If I am using window.location.href but it is giving me page reloading experience. How can i prevent reloading and redirect to new URL(path from navigate to URL)

function App(){

useEffect(()={retreiveCategoryLis();})

const retreiveCategoryLis= ()={//axios call to get the category List}

 const navigateToURL = (category) =>
    {
        const path = 'http://localhost:4200/Categories?categoryName='+category;
        alert(path);
        //window.location.href=path
        
    }

 return(
    <div className="categoriesContainer">
        {categoryList && categoryList.map((category) => (
            <div key={Math.random()} className='categoryItem' onClick={()=> navigateToURL(category)}>
            <img key={Math.random()} src={getCategoryImage(category)} alt=""></img>
            <p key={Math.random()}> {category}</p>
        </div>
        )
        )}
    </div>
}

Upvotes: 0

Views: 87

Answers (1)

remonke
remonke

Reputation: 376

ok since you use react, you can use a library that called react-router-dom.

Upvotes: 2

Related Questions