React Nested Routes throwing error - Meteor - reactjs

Trying to setup a simple global layout (App), and have the path /apply render within App.
I keep getting the warning:
You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
Which I can tell 100% applies to the situation, but Google yielded no relevant answers/solutions to my problem.
Routes.jsx
import React from 'react';
import { render } from "react-dom";
import { Router, Route, IndexRoute } from "react-router";
import createBrowserHistory from "history/createBrowserHistory";
const history = createBrowserHistory();
// route components
import App from "./App.jsx";
import ApplyPage from "./pages/ApplyPage.jsx";
export const renderRoutes = () => (
<Router history={history}>
<Route path="/" component={App}>
<Route path="apply" component={ApplyPage}/>
</Route>
</Router>
);
App.jsx
import React, { Component } from 'react'
import TopBar from "./components/TopBar.jsx"
import LeftMenuContainer from "./components/LeftMenuContainer.jsx"
import LivePurchases from "./components/LivePurchases.jsx"
export default class App extends Component {
render() {
console.log(this.props);
return (
<div className="App">
<div className="flexWrapperGlobal">
<TopBar/>
<div className="contentContainer">
<LeftMenuContainer/>
<div className="bodyContainer">
<LivePurchases/>
<div className="siteContentContainer">
{this.props.children}
</div>
</div>
</div>
</div>
</div>
)
}
}
ApplyPage.jsx
import React, { Component } from 'react'
export default class ApplyPage extends Component {
render() {
return (
<div className="applyContainer">
<div className="applySubContainer">
<div className="applyBlock">
<h4>Seller Application</h4>
<form>
<h5>Roblox Account</h5>
<input type="text" placeholder="Username"/>
<h5>Selling Experience</h5>
<textarea type="text" placeholder="Selling Experience"/>
<h5>Extra Information</h5>
<textarea type="text" placeholder="Extra Information"/>
</form>
<a className="btn">Send</a>
</div>
</div>
</div>
)
}
}

I ran into this same issue today, found the solution here: Nested Routes in v4
<App>
<Route ....... />
<Route ....... />
</App>
This worked for me.

Found the answer, in case anyone's ran into the same examples.
You now nest the routes into your rendered top-level object, in my case App.
So you have to move <Route path="/apply" component={ApplyPage} /> to App.jsx, like this:
render() {
return (
<div className="App">
<div className="flexWrapperGlobal">
<TopBar/>
<div className="contentContainer">
<LeftMenuContainer/>
<div className="bodyContainer">
<LivePurchases/>
<div className="siteContentContainer">
<Route path="/apply" component={ApplyPage} />
</div>
</div>
</div>
</div>
</div>
)
}

Related

react router dom don't work to access some component

Good morning community StackOverflow.I had to use the route to access some component inside the application but that's not work for me despite I had installed the "npm install react-router-dom" the browser render me a blank file,so this is all my file :
app.js file :
import React from 'react';
import HomeScreen from './screens/HomeScreen';
import ProductScreen from './screens/ProductScreen';
import {BrowserRouter, Route, Switch} from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<div className="grid-container" >
<header className="row" >
<div>
<a className="brand" href="/">My Shop</a>
</div>
<div>
Cart
Sign In
</div>
</header >
<main>
<Route path="/" component={HomeScreen} ></Route>
<Route path="/product/:id" component={ProductScreen} ></Route>
</main>
<footer classNameName="row center" >All right reserved</footer>
</div>
</BrowserRouter>
);
}
export default App;
this is the HomeScreen file :
import React from "react";
import {data} from '../data';
import Product from '../components/Product';
function HomeScreen () {
return (
<div>
<div className="row center">
{data.products.map((product) => (
<Product key={product._id} product={product} ></Product>
))}
</div>
</div>
)
};
export default HomeScreen;
this is the ProductScreen file :
import React from "react";
function ProductScreen () {
return (
<div>Product Screen</div>
)
};
export default ProductScreen;
this is the Product file code:
import React from 'react';
import Rating from './Rating';
function Product (props) {
const {product} = props;
return (
<div className="card">
<a href="product.html">
<img className="medium" src={product.image} alt={product.name} />
</a>
<div className="card-body">
<a href="product.html">
<h2>{product.name}</h2>
</a>
<Rating rating={product.rating} numReviews={product.numReviews} ></Rating>
<div className="price" >${product.price}</div>
</div>
</div>
)
}
export default Product;
In react router v6, lots of keywords have changed. In addition, various functions like useHistory have also been changed. Check the package version in package.json file. You can also check your browser console for errors.
In case you have v6 or above, change your app.js as below, to make it work. I have changed the react router keywords, indented code and made Route tag as self closing.
import React from 'react';
import HomeScreen from './screens/HomeScreen';
import ProductScreen from './screens/ProductScreen';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
function App() {
return (
<Router>
<div className="grid-container" >
<header className="row" >
<div>
<a className="brand" href="/">My Shop</a>
</div>
<div>
Cart
Sign In
</div>
</header>
<main>
<Routes>
<Route path="/" element={<HomeScreen />} />
<Route path="/product/:id" element={<ProductScreen />} />
</Routes>
</main>
<footer classNameName="row center" >All right reserved</footer>
</div>
</Router>
);
}
export default App;
If you're using version 6, you have to wrap <Route> components with <Routes> (you should import the component first), otherwise, wrap them with <Switch> component

Unable to see the text returned from home and tutorials page in react-hooks site

Unable to see the header text returned in my Home and Tutorials page in my react hooks site. Below is my App.js, Navigation.js and Home.js. Could someone please advise about the problem here.
App.js
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import './App.css';
import Home from "./components/Home";
import Tutorials from "./components/Tutorials";
import Navigation from './components/Navigation';
function App() {
return (
<BrowserRouter>
<Navigation>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</Navigation>
</BrowserRouter>
);
};
export default App;
Navigation.js
import React from 'react';
import { NavLink} from 'react-router-dom';
const Navigation = () => {
return (
<div className="App">
<div className="wrapper">
<div id="wrap">
<nav className="siteNavigation_nav_links">
<div className="row" ></div>
<div className="main_links_nav">
<NavLink className="mob_link" to="/">Home</NavLink>
<NavLink className="mob_link" to="/tutorials">Tutorials</NavLink>
</div>
</nav>
</div>
</div>
</div>
)
}
export default Navigation;
Home.js
import React from 'react';
const Home = () =>{
return (
<div className="App">
<h1>This is Home Page !</h1>
</div>
)
}
export default Home;
Tutorials.js
import React from 'react';
const Tutorials = () =>{
return (
<div><h1>This is Tutorials Page !</h1></div>
)
};
export default Tutorials;
Navigation is a Navbar component. And you want it everywhere on the site.
So, you can easily do it by using it outside the scope of the Switch component.
In doing so, it will render all over your screens.
<BrowserRouter>
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/tutorials" component={Tutorials} />
</Switch>
</BrowserRouter>

Designing a proper Navigation Bar in React?

I am trying to create a following app with NavBar in React. However I cannot render the element on the screen after clicking the hyper-link. Everything displays correctly in my browser however I cannot open render new element under the nav-bar after clicking the NavLink.
App.ts My main component
import React from "react";
import ReactDOM from "react-dom"
import Navbar from "./Navbar"
import Form from "./Form"
import Messages from "./Messages"
import { BrowserRouter } from "react-router-dom"
// STYLE
class App extends React.Component {
render() {
return (
<BrowserRouter>
<div className="App">
<Navbar />
<Form />
</div>
</BrowserRouter>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Navbar.tsx Navigation bar containing only messages tab at this moment
import React from "react";
import { Route, Link } from "react-router-dom";
import Messages from "./Messages"
const topBarS = {
background: 'cyan',
height: '50px',
width: '100%%',
};
class Navbar extends React.Component {
render()
{
return(
<div style={topBarS}>
<nav>
<Link to="/Messages">Messages</Link>
</nav>
<Route
path="/Messages"
component={Messages}
/>
</div>
);
}
}
export default Navbar;
Messages.tsx New tab that should display right after clicking Messages NavLink
import React from "react";
import ReactDOM from "react-dom"
class Messages extends React.Component {
render() {
return (<h4>You have no messages</h4>)
}
};
export default Messages;
Form.tsx Form
import React from "react";
import ReactDOM from "react-dom"
const Form = () => (
<form method="POST">
<label htmlFor="name">Name</label>
<div />
<input type="text" name="name" />
<div />
<label htmlFor="title">Title</label>
<div />
<input type="title" name="title" />
<div />
<label htmlFor="message">Message</label>
<div />
<textarea name="message" ></textarea>
<div />
<input type="submit" />
<div>
</div>
</form>
)
export default Form;
You can use Route inside the BrowserRouter like this :
<BrowserRouter>
<div className="App">
<Navbar />
<Route exact path='/' component={Home} />
</div>
</BrowserRouter>
What you need here is an AppRouter that has all the routes in your page wrapped in a <Switch> component like so:
<BrowserRouter>
<div className="App">
<Navbar />
<Switch>
<Route
path="/Messages"
component={Messages}
/>
<Route
path="/"
component={Home}
/>
</Switch>
</div>
</BrowserRouter>
And inside the Navbar just leave the Links and drop the Route components.
Good Luck

You should not use Link outside a Router using react-router-dom

I am trying to do routing using react-router-dom. I got an error like you should not use outside the like this.Here below i am attaching my code please check.
index.js
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createLogger } from 'redux-logger'
import App from './containers/App';
render(
<Provider>
<App/>
</Provider>,
document.getElementById('root')
)
App.js
import React from 'react';
import Dropdown from './dropdown';
import './styles.css';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import Home from './Home';
import Addatttemp from './Addatttemp';
const options = [
{label: 'One' },
{label: 'Two'},
{label: 'Three'},
{label: 'Four' }
]
const AgentValues=[
{label: 'Add Att/temp', value:'Addatttemp' },
{label: 'Mod prof LVL',value:'Modproflvl'},
{label: 'Override Attr',value:'Overrideattr'},
{label: 'Temp',value:'temp'}
]
const defaultOption = options[0]
export default class App extends React.Component {
constructor (props) {
super(props)
}
render() {
return (
<div>
<div className="navbar">
<div className="align">
<div className="dropdown">
<Dropdown options={AgentValues} name='Agent'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Templete'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Report'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Search'/>
</div>
</div>
</div>
<Router>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/Agent/Addatttemp' component={Addatttemp} />
</Switch>
</Router>
</div>
);
}
}
Dropdown.js
import React, { Component, PropTypes } from "react";
import { Link } from 'react-router-dom';
class dropdown extends Component {
constructor (props) {
super(props)
}
render(){
var dropdownList = []
this.props.options.map((res)=>{
dropdownList.push(<Link to={'/'}>{res.label}</Link>)
})
return(
<div>
<button className="dropbtn"><Link to={'/'}>{this.props.name}</Link>
</button>
<div className="dropdown-content">
{dropdownList}
</div>
</div>
)
}
}
export default dropdown;
Home.js
import React from 'react'
const Home = () => (
<div>
<h1>Welcome to the Tornadoes Website!</h1>
</div>
)
export default Home
Addatttemp.js
import React from 'react'
const AddTemp = () => (
<div>
<h1>Welcome to the Add att/temp!</h1>
</div>
)
export default AddTemp
Like this i wrote my code, but when i run this code it throws me the error like Uncaught Error: You should not use Link outside a Router . I am unable to resolve this, please give me suggestions that what i did wrong in it, Any help much appreciated.
As the error message says you are not having Router up the component tree.
You could use it in App component like this
export default class App extends React.Component {
render() {
return (
<Router>
<div>
<div className="navbar">
<div className="align">
<div className="dropdown">
<Dropdown options={AgentValues} name='Agent'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Templete'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Report'/>
</div>
<div className="dropdown">
<Dropdown options={options} name='Search'/>
</div>
</div>
</div>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/Agent/Addatttemp' component={Addatttemp} />
</Switch>
</div>
</Router>
);
}
}
Also
You don't need constructor that just calls super
// this is noop
constructor (props) {
super(props)
}
You don't need to push to external array when using map.
var dropdownList = this.props.options.map(res => (
<Link to={'/'}>{res.label}</Link>
))
If you are trying to show a Link on a page or any element outside of BrowserRouter you are going to get this error. This error message is saying that any component that is not a child of a router cannot contain any react-router-dom related components.
What you need to learn for effective development in React is your component hierarchy.
In your case your parent component, the one at the top of the hierarchy is App, how do I know? because you said so right here in your index.js file:
render(
<Provider>
<App/>
</Provider>,
document.getElementById('root')
)
So, when working with react-router-dom, next up on that hierarchy chart should be your BrowserRouter. This is where you start to lose me and React as we are both looking for BrowserRouter inside your App component and do not see it.
This is what we should be seeing right now:
const App = () => {
return (
<div className="ui container">
<BrowserRouter>
<div>
<Route path="/" exact component={Home} />
<Route path="/Agent/Addatttemp" exact component={Addattemp} />
</div>
</BrowserRouter>
</div>
);
};
Notice I am using the exact keyword? I am using the exact keyword so I don’t accidentally match one of these other routes I am about to implement.
So please go ahead and add your BrowserRouter or in your case, your Router which is acting as BrowserRouter higher up on that hierarchy below App, it has to be next up in the hierarchy, everything else is a child of that BrowserRouter and must go within it.
And since you are never going to make use of the Link from react-router-dom inside your App component, go ahead and clean it up like so:
import { BrowserRouter, Route } from "react-router-dom";
Once you follow the above, that error should go away.

activeClassName of react-router v4 not working appropriately

I am very new to react-routing. After reading the docs and reading some articles this is how structured my routing. Please correct me if I am doing wrong.
Using react-router V4
Routes.js
import React from 'react';
import App from '../app/components/App';
import Dashboard from '../dashboard/components/Dashboard';
import Contact from '../dashboard/components/Contact';
import Account from '../dashboard/components/Account';
import Career from '../dashboard/components/Career';
import NoMatch from './NoMatch';
import { Provider } from 'react-redux';
import { Route, BrowserRouter, Switch, Redirect } from 'react-router-dom';
const Root = ({ store }) => (
<Provider store={store}>
<BrowserRouter>
<div>
<Route path="/app" component={App} />
<Switch>
<Route path="/app" exact component={Dashboard} />
<Route path="/app/home/dashboard" component={Dashboard} />
<Route path="/app/home/contact" component={Contact} />
<Route path="/app/home/account" component={Account} />
<Route path="/app/home/career" component={Career} />
<Route component={NoMatch} />
</Switch>
</div>
</BrowserRouter>
</Provider>
)
export default Root
I used /app 2 times. First is to load always as it has sidenav and header. Then inside switch I used to load default component dashboard.
App.js
import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.css';
import 'font-awesome/css/font-awesome.min.css';
import Header from './Header';
import SideNav from './SideNav';
class AppComp extends Component {
componentDidMount() {
const { match: { params } } = this.props;
}
render() {
return (
<div>
<div className="container body">
<div className="main_container">
<Header />
<SideNav routeparams={this.props}/>
</div>
</div>
</div>
);
}
}
export default AppComp;
Sidenav.jsx
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom'
import '../../css/sidenav.css';
class SideNav extends Component {
render() {
console.log(this.props.routeparams);
return (
<div className="col-md-3 left_col">
<div className="left_col">
<div className="clearfix"></div>
<div id="sidebar-menu">
<div className="menu">
<ul className="nav side-menu">
<li>
<NavLink to="/app/home/dashboard">Home</span></NavLink>
<ul>
<li className="current-page">
<NavLink to="/app/home/dashboard" activeClassName="current">Dashboard</NavLink>
</li>
<li>
<NavLink to="/app/home/contact" activeClassName="current">Contact</NavLink>
</li>
<li>
<NavLink to="/app/home/account" activeClassName="current">Account</NavLink>
</li>
<li>
<NavLink to="/app/home/career" activeClassName="current">Career</NavLink>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
);
}
}
export default SideNav;
I have two issue :
this.props.routeparams in sidenav logged twice which means sidenav rendered twice . this is happening after adding routing . Also this.props.routeparams match path is always /app, which I think because sidenav is a child component of app component. How can I fix this ? I want to get current route path in sidenav.
activeClassName="current" gets applied to correct navlink but the css style gets reflected only if I click somewhere in the page. Seems so strange. I can resolve that issue if I get current match.path at sidenav component then I will do it custom way without activeClassName.
Any help would be greatly appreciated.

Resources