I'm new to React and React Router and I am facing some trouble regarding the styling of the routes. So basically I have 2 routes: the main page and the admin page. All I want to do is to style the body so that in the admin page everything is centered. The problem is that each time i style the body, all the styling goes to the main page too. So how can I fix this issue?
You might me applying styling to you app.js file where BrowserRouter is implemented. Here is what you can do. Create your Home/Main page as:
import React from "react";
const Home = () => {
return <div style={{textAlign: "center" }}>Home</div>
}
export default Home;
Create your Admin page as:
import React from "react";
const Admin = () => {
return <div>Admin</div>
}
export default Admin;
And finally, Create your app.js file as:
import React from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import Admin from "./Admin";
import Admin from "./Home";
const App = () => {
return <BrowserRouter>
<Switch>
<Route path="/admin" component={Admin} />
<Route path="/" exact component={Home} />
</Switch>
</BrowserRouter>
}
export default App;
In this way, your only your Home page content is aligned centered. Hope this will help you.
Related
I'm trying to make a little React App and it is the first time I'm using Router.
import { render } from "react-dom";
import {
BrowserRouter,
Routes,
Route
} from "react-router-dom";
import App from "./App";
import Shop from './routes/shop';
import style1 from "./App.css";
import style2 from "./shop.css";
const rootElement = document.getElementById("root");
render(
<BrowserRouter>
<div style={style1}><Routes>
<Route path="/" element={<App/>} />
</Routes></div>
<div style={style2}><Routes>
<Route path="shop" element={<Shop />}/>
</Routes></div>
</BrowserRouter>,
rootElement
);
It works great and I can use http://localhost:3000/ and http://localhost:3000/shop, but it is not switching the css files.
both CSS files get loaded on both pages
I know that React is a One Page Application, but please tell me how to remove the frickin App.css from http://localhost:3000/shop
So I want App.css on http://localhost:3000/ and Shop.css on http://localhost:3000/shop
I would be very grateful for every answer!
To add onto what Shivam said, what's happening with your code currently is that React is importing both CSS files and applying them.
This is because the syntax you're using is incorrect. You can't import a css file as something else:
import styles1 from "./App.css"
is actually just importing App.css and applying it to App.js and Shop.js.
You can do what Shivam said, however, if you want to style your react app in the way you were trying to, you can use css modules.
These work in the way that you were trying to use a css file - you can import them and use inline styling. I wouldn't recommend this approach however, since you would have to style every JSX expression in 'App.js' in this way. Just setting 'style={style}' only passes a prop to App.js with that style component, it doesn't actually apply it.
Check out this link. It covers css modules and some other ways of styling css in react. Best!
I don't why you're stating the CSS styles while browsing Routes.
Just import your CSS files in your component.js
Like In your case,
import { render } from "react-dom";
import {
BrowserRouter,
Routes,
Route
} from "react-router-dom";
import App from "./App";
import Shop from './routes/shop';
const rootElement = document.getElementById("root");
render(
<BrowserRouter>
<Routes>
<Route path="/" element={<App/>} />
</Routes>
<Routes>
<Route path="shop" element={<Shop />}/>
</Routes>
</BrowserRouter>,
rootElement
);
This is your components,
import "./App.css";
import React from 'react';
export default function App(){
//YOUR CODE HERE
}
Another Shop Component
import "./shop.css";
import React from 'react';
export default function Shop(){
//YOUR CODE HERE
}
This'll work Fine!
I have a react.js site created using: https://facebook.github.io/create-react-app/docs/getting-started with the npx create-react-app my-app command. I got many pages in my app and when I open the rendered source code in google chrome after building the site. The problem is all pages are in one bundle.js including all text and HTML elements of all pages are in that file combined. Does someone know how to get the HTML rendered in the source code for all pages individually instead of having just a bundle.js for all pages content?
So the other pages can get indexed individually by google?
I hope someone knows how to get this working. If there is an npm plugin or an example site available please post the link. I really can't find a solution for weeks now, I just see a bundle.js in the exported HTML file that includes all text and images and HTML elements of all pages how can I have it rendered as html output instead of just all in one bundle.js for indexing purposes?
By the way: my app uses 'react-router-dom' I don't want to break, the routing of the pages but having the pages being indexable individually.
Below is a sample code
App.js
//App.js
import React, { Component } from 'react';
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom';
import Nav from './components/nav';
import Header from './components/header';
import Footer from './components/footer';
import home from './components/pages/home';
import about from './components/pages/about';
import contact from './components/pages/contact';
class App extends Component {
render() {
return (
<Router>
<div className="App">
<Nav/>
<Header/>
<Route exact path="/" component={home} />
<Route exact path="/about" component={about} />
<Route exact path="/contact" component={contact} />
<Footer/>
</div>
</Router>
);
}
}
export default App;
home.js
//home.js
import React, { Component } from 'react';
class home extends Component {
render() {
return (
<div className="container">
<p>content...</p>
</div>
);
}
}
export default home;
I'm new to React Router so if this has been asked before maybe someone could point me in the right direction! Basically I have a WordPress install that I'm pulling in my websites data from through the API.
I've created custom routes to query my pages and my posts by slug.
Using react router I was able to create a template called Page.js which changes dynamically using the code below.
However, now I'm trying to do the same exact thing with the blog posts but the app isn't using Blog.js its still defaulting back to Page.js
here's my App.js code...
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";
import Home from './pages/Home';
import Page from './pages/Page';
import Blog from './pages/Blog';
import Header from './components/Header';
import Footer from './components/Footer';
class App extends React.Component {
render() {
return (
<Router>
<div>
<Header/>
<Route exact path="/" component={Home} />
<Route path="/:slug" component={Page} />
<Route path="/blog/:slug" component={Blog} />
<Footer/>
</div>
</Router>
);
}
}
export default App;
More Details:
Page.js works by checking const { slug } = this.props.match.params; and then querying WordPress using that slug to pull in the data it needs. In componentDidUpdate i'm checking prevProps to see if the slug matches the previous slug, if not it fetching the new data.
This works great and I was hoping to do the same in the Blog.js as well.
However, if this isn't the best approach please advise another method.
Two things:
Use element: This will allow only one route to be used, no composing. (See this documentation)
Check the order of path statements: Use defined paths before :param, this avoids considering /blog/:slug as a /:slug parameter.
`
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
class App extends React.Component {
render() {
return (
<Router>
<div>
<Header/>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/blog/:slug" component={Blog} />
<Route path="/:slug" component={Page} />
</Switch>
<Footer/>
</div>
</Router>
);
}
}
I think you're pretty close to the recommended implementation, just a few small tweaks should get you there.
First,
In your App.js file you're actually handling routing, without using the <Switch> component provided by React Router, replacing the <div> and </div> tags in your App.js file with <Switch> and </Switch> respectively should get this working for you. See below...
import React from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom"; //make sure you import it also!
import Home from './pages/Home';
import Page from './pages/Page';
import Blog from './pages/Blog';
import Header from './components/Header';
import Footer from './components/Footer';
class App extends React.Component {
render() {
return (
<Router>
<Switch> //Add this in
<Header />
<Route exact path="/" component={Home} />
<Route path="/blog/:slug" component={Blog} />
<Route path="/:slug" component={Page} />
<Footer />
</Switch> //Add this in
</Router>
);
}
}
export default App;
I would recommend going further though!
To make these components more understandable, you should refactor routing functionality into a routes.js file, and top-level App component logic/structure into the App.js file. See below...
In App.js:
This file is where you should handle your base application structure and logic. For example this file is where you'll import your <Header>, your <Footer>, and where the Route component will render.
import * as React from 'react'
import Header from './../Header/Header.jsx'
import Footer from './../Footer/Footer.jsx'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
// Handle your top-level application state here
}
}
// define your top-level application functions here
render() {
return (
<div>
<Header />
<main>
{this.props.children} //This where the Route components will render
</main>
<Footer />
</div>
)
}
}
export default App
In Routes.js:
This file is where you should import your App component, and then handle the routing statements.
import React from 'react'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import App from './components/App'
import Home from './pages/Home'
import Page from './pages/Page'
import Blog from './pages/Blog'
/* construct routes */
export default () => {
return (
<Router>
<App>
<Switch>
<Route path='/' exact component={Home} />
<Route path='/blog/:slug' component={Blog} />
<Route path='/:slug' component={Page} />
</Switch>
</App>
</Router>
)
}
If you structure your application this way, your routing logic and top-level application logic are separate, and in the end your files will be less cluttered as both Route files and top-level App files can get fairly dense.
Hope this helps! Let me know if I can explain anything further.
I am using react-router and having some difficulties with it's behaviour.
The Nav shows on all pages as desired. However, the Profile shows on all pages too. I only want to show this on /home and also on the /music and /players pages, which it does. However, it also shows on the /charts page which is confusing me.
My code looks like the following.
import React from 'react';
import { Route } from 'react-router-dom'
import Nav from './components/Nav'
import Profile from './components/Profile'
import Players from './components/Players'
import Music from './components/Music'
import Charts from './components/Charts'
const App = () => {
return (
<section>
<Nav />
<Route path="/home">
<div>
<Profile avatarUrl={ avatarUrl }/>
<Route path="/players" component={Players}/>
<Route path="/music" component={Music}/>
</div>
</Route>
<Route path="/charts" component={Charts}/>
</section>
)
}
export default App;
I have read through the docs, tried putting in a Switch component, added exact to the home route but this leads to other unexpected behaviour.
Can anyone advise what I am doing wrong?
Thanks Pete!
Try this:
import React from 'react';
import { Route, BrowserRouter as Router } from 'react-router-dom'
import Nav from './components/Nav'
import Profile from './components/Profile'
import Players from './components/Players'
import Music from './components/Music'
import Charts from './components/Charts'
const Home = ({match}) => {
return (
<div>
<Profile avatarUrl={ avatarUrl }/>
<Route path=`${match.url}/players` component={Players}/>
<Route path=`${match.url}/music` component={Music}/>
</div>
);
};
const App = () => {
return (
<section>
<Nav />
<Router>
<Switch>
<Route path="/charts" exact={true} component={Charts}/>
<Route path="/home" component={Home} />
</Switch>
</Router>
</section>
)
}
export default App;
I haven't tested this, but this should work.
Assuming that you're using react-router v4, I don't know if you can actually use your home route in the way you've used it.
In the code above, Switch basically renders the first match between the routes specified under it. The exact keyword will ensure that only /charts path will display the Charts component.
The Home component will render in any path that starts with /home.
Now, for path /home/players, you'll see the Profile and the Players component, whereas for path /home/music, you'll see the other combination.
Hope this helps. :)
Edit:
Added Router to the code.
Edit:
Working code available here: https://codesandbox.io/s/8x9pql9m19
Change route on right hand side to:
/home
/home/players
/home/music
/charts
Using a similar React-Route from a previous project that worked but this one just gives a white screen, if i make a nav link in the root directory it changes the path but stays white. not sure why the content is not loading in. also have the code on repo: repo with code
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import App from './App';
import Test from './components/test';
import Home from './pages/home_page';
import './index.css';
ReactDOM.render((
<Router history={browserHistory}>
<Route path='/' component={App}>
<IndexRoute component={Home}/>
<Route path='/test' component={Test} />
</Route>
</Router>
), document.getElementById('app'))
App.js
import React, { Component } from 'react';
import './App.css';
export default class App extends Component {
render() {
return (
<div>
{this.props.childen}
</div>
);
}
}
Just adding that the console has no errors and says it was compiled correctly with no warnings. The url does change but the content is not loaded to the page.
I cloned your repo and discovered the issue - it's a typo in App.js
{this.props.childen} -> {this.props.children}
It works now.