React-router not loading css for nested pages on refresh

后端 未结 7 737
甜味超标
甜味超标 2020-12-25 12:07

I\'m trying to setup a react-router for my first React webapp, it seems to be working except that the css doesn\'t load for my nested pages when I refresh the p

7条回答
  •  没有蜡笔的小新
    2020-12-25 12:32

    If you are using create-react-app workflow, put the assets under public folder and use the special variable PUBLIC_URL.

    Inside index.html use %PUBLIC_URL%:

    Inside JS/JSX files use process.env.PUBLIC_URL:

    render() { // Note: this is an escape hatch and should be used sparingly! // Normally we recommend using import for getting asset URLs // as described in “Adding Images and Fonts” above this section. return ; }

    Recommended Approach:
    import stylesheets, images, and fonts from JavaScript by placing it along with src files.

    import React from 'react';
    import logo from './logo.png'; // Tell Webpack this JS file uses this image
    
    console.log(logo); // /logo.84287d09.png
    
    function Header() {
       // Import result is the URL of your image
       return Logo;
    }
    
    export default Header;
    

    Adding assets to public folder

    When to use public folder

提交回复
热议问题