how to render to a page whenever user click to a dynamic url ReactJS?

后端 未结 2 906
野趣味
野趣味 2021-01-28 15:17

what i am trying to solve is, how to render to a specific page whenever user click to dynamic url? for more specific, there is my \"product_list\" api data. in \"product_list\"

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 15:42

    For dynamic URLs, you may not want to go with absolute paths. Consider shifting to query parameters.

    Change url to http://localhost:8000/api/p?id=product01. In routing, render ProductDetailContainer for url 'http://localhost:8000/api/p'.

    function ProductDetailContainer() {
        const query = new URLSearchParams(useLocation().search); // useLocation in react-router
    
        return ;
    }
    
    function ProductDetail({id}) {
        return ({id}); // Render details based on the ID fetched.
    }
    

    Code above is not tested. Please adapt accordingly :)

    Hope it helps.

提交回复
热议问题