I'm poking around with react routing and can't get it to work. I've stripped down to an empty new project just to test the fundamentals, and still can't get it running. I am using vscode and generated my react project using npx. This is my current code:
App.js
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import './App.css';
import Landing from './components/Landing'
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path='/' element={<Landing />} />
</Routes>
</BrowserRouter>
);
}
index.js
import React from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
My landing.js is just a text entry, which I have confirmed does work if there is no BrowserRouter wrapper.
I've tried:
Adding <h1>Hello</h1> within <BrowserRouter> to see if it would render prior to the Routes -- it did not.
Varying syntaxes (component vs element keyword, different paths)
Leaving index.js as default and doing routing in App.js
Leaving App.js as default and doing routing within the render function in index.js
All of the resources I've found online for react-router-dom#6 points that this (or some iteration of what I've done) is correct, yet I get a blank screen no matter what I do once I wrap within <BrowserRouter>.
Any help would be invaluable at this point; I don't think I'll be able to continue on my own.
After creating the project with npx create-react-app bin the next step should be to cd into the bin directory and run npm install to install dependencies, then npm i -s react-router-dom to install react-router-dom and save it to your package.json file, then run npm start to run the app. I think the node version installed should be fine.
Steps:
Create project: npx create-react-app bin
Change into the project directory: cd bin
Install project dependencies: npm i
Add react-router-dom to the project: npm i -s react-router-dom
Edit code & save
Run the project: npm start
Related
I created a new React JS app using the Visual Studio 2022 Project Template "Standalone JavaScript React Template"
When I run the project I see the react logo just like when you use "create-react-app" (I'm assuming that Visual Studio 2022 creates the project in the same way)
I want to use Bootstrap in my project so I installed the module by going to the root directory of the project and typing "npm install bootstrap" I can see that the module is installed in the package.json
When I try and import bootstrap in my index.js using this line:
import "/bootstrap/dist/css/bootstrap.min.css";
Then run the app I get this error:
Module not found: Error: You attempted to import /bootstrap/dist/css/bootstrap.min.css which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/
My question is what is the correct way to import and use bootstrap in my project, did I install it in the right way or is there a diffrent method?
This is my full index.js file for clarity:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import "/bootstrap/dist/css/bootstrap.min.css";
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.
reportWebVitals();
UPDATE: See sctructure of project
Here is my code for the index.js page:
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter as Router, Route, Switch} from "react-router-dom"
import './css/bootstrap.min.css';
import './css/style.css';
// Pages
import Library from './pages/Library.js'
import Register from './pages/Register.js'
ReactDOM.render(
<div>
<Router>
<Switch>
<Route exact path="/" component={Library}/>
<Route exact path="/library" component={Library}/>
<Route exact path="/register" component={Register}/>
</Switch>
</Router>
</div>,
document.getElementById('root')
)
When I run npm start I get the application as desired. localhost:3000 and localhost:3000/library render the Library component, while localhost:3000/register renders the Register component.
When I run npm run build I get the build folder but when I enter the index.html page, it is blank.
This problem was introduced after I implemented the router. Previously the result of both npm start and npm run build were the same.
Here is the link to the same issue you are facing on the official create-react-app github issues
It is marked as closed there, you can also read that for more info.
You should serve the build folder for example like
npx serve -s build
It seems like I followed all the necessary in the documentation but it keeps giving a 404 Error: "There isn't a GitHub pages here" when I go into https://juliesong.me.
I got my custom domain through GoDaddy and configured the DNS like so:
Then I changed my package.json file to have
"homepage": "https://www.juliesong.me",
Added a CNAME file in the root directory containing
www.juliesong.me
And lastly went into settings in GitHub pages:
I searched my issue up and a lot of people said it might have to do with the React Router, so I edited to include a basename:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { HashRouter as Router } from "react-router-dom";
import * as serviceWorker from './serviceWorker';
const routing = (
<Router basename={process.env.PUBLIC_URL}>
<App />
</Router>
);
ReactDOM.render(routing, document.getElementById('root'));
This is my App.js:
import React, { Component } from 'react';
import {Route} from 'react-router-dom';
import { withRouter } from 'react-router'
import { GlobalStyles, } from "./util/GlobalStyles";
import Home from './containers/Home'
class App extends Component {
render() {
const home = () => <Home />
return (
<main>
<GlobalStyles />
<React.Fragment>
<Route exact path="/" component={home} />
</React.Fragment>
</main>
);
}
}
export default withRouter(App);
If anyone could help on this issue, that would be great:)
Try this one
add new file named .htaccess in your build folder
example of file
and add this code into your .htaccess file
the code
Unless you share how you are generating the build and pushing it to GitHub Pages, it is very difficult to identify the cause of this issue.
Still let me share a general way you would deploy a react app in GitHub pages.
You don't need to add basename in your router
Good thing is you are using HashRouter instead of BrowserRouter, because GitHub pages doesn't support history api like normal browser, it will look for a specific file in given path
To deploy your app you need to run the following command in same order -
npm run build # this will generate a build folder
gh-pages -d build # this will publish the build folder to GitHub pages
This is very important that you publish the build folder, because
its contains the index.html file, which GitHub will point.
You can read more about deploying react app to GitHub Pages here
React-app out of the box is client-server application, and you should compile it first to html + client side js before deploying.
There are lots of good docs: create-react-app.dev, medium.com and the shortest way here
I have been having a hard time pushing a React.js app to Heroku because I keep getting a failed to build message with the following amplifying information.
Heroku Build Console:
-----> Build
Running build
> veterinfo#0.1.0 build /tmp/build_2456efea678b82d24203cb2e2c7bcbd5
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/index.js
Cannot find file './containers/App/App' in './src'.
My index.js file:
import App from './containers/App/App'
import * as serviceWorker from './serviceWorker'
import { BrowserRouter } from 'react-router-dom'
import { Provider } from 'react-redux'
import { createStore, applyMiddleware } from 'redux'
import rootReducer from './reducers'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'
const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(thunk)))
const router = (
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
)
File Structure:
src
-->containers
-->App
-->__snapshots__
-->App.js
-->App.scss
-->App.test.js
-->index.js
I have tried deleting node_modules and reinstalling them,
I have tried clearing the build cache in heroku,
I have tried specifying App.js in the relative path,
I have tried specifying the node version in the yml and package.json files,
The Build passes locally with npm run build,
I have tried just passing in each component into the router's position and I have found that all of the components work but all of the containers do not (could this be a redux issue somehow?)
I am about out of ideas :/
You can start by clearing the cache and see if there are any files that have been renamed.
This will cache the entire directory:
git rm -r --cached .
Also, you have to add, commit and push the changes:
git add .
git commit -am 'Removing cached files'
Rename containers to Containers and components to Components
You might have changed the casing of the file names after you have commited to git. You can fix it by calling git mv path/app.js path/App.js. This explicitly changes the file name in git.
The problem was fixed when I renamed the directory to Containers and the relative path to import App from './Containers/App/App'
Check .gitignore , may be your files are not being sent to remote repository
This is the first time I tried react.
I create a project using create-react-app, then for routing, I use React Router V4 for web react-router-dom.
This is my index.js file
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
);
There's no problem when I start the server with npm start. But when I build the app with npm run build, putting it in my www folder in my Apache server and access it, I only get empty page with <!-- react-empty: 1 --> inside my root div.
<div id="root">
<!-- react-empty: 1 -->
</div>
I have tried looking for similar cases, but the answer always about adding historyApiFallback in webpack.config.js or gulp, which I can't find anywhere in the create-react-app template.
I have tried HashRouter, it's working fine, but there's /#/ in the url which is undesirable.
How can I get the BrowserRouter to work after I build the app?
Well, there was no problem at all. It worked, even if it was built using npm run build. My best guess is: you placed the files in the wrong folder.
When you build your code using npm run build, the static folder should be placed inside the MallApp folder. So your folder structure should be like this:
/var/www/mywebfolder/index.html
/var/www/mywebfolder/MallApp/static
Check your browser's console, I believe it contains errors. Failed to get the js and css files.