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
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
;
}
export default Header;
Adding assets to public folder
When to use public folder