Hello i am new in react js.
import React, { Component } from 'react';
import { Route, Switch, Redirect } from 'react-router-dom';
import ProductComponent from './Product/productComponent';
import Navbar from './component/NavBar';
import ProductAction from './component/ProductAction';
class App extends Component {
state = { }
render() {
return (
<React.Fragment>
<Navbar />
<main className="container">
<Switch>
<Route path="/product" exact component={ProductComponent}/>
<Route path="/product-action" exact component={ProductAction}/>
</Switch>
</main>
</React.Fragment>
);
}
}
export default App
This is my app component. Here i have used a NavBar component to distribute links and in route i have used ProductComponent component but ProductComponent is not a child of app component or it may be i don't know. My question is how can i send data to NavBar component from ProductComponent component.
Here is my Navbar component.
import React, { Component } from 'react';
import { Link, NavLink } from 'react-router-dom';
class NavBar extends Component {
render() {
return (
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">
Navbar
</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<NavLink className="nav-link" to="/product">
Product
</NavLink>
</li>
<li class="nav-item">
<NavLink className="nav-link" to="/product-action">
Product action
</NavLink>
</li>
</ul>
</div>
</nav>
);
}
}
export default NavBar;
You can use the React useContext hook to share data between two components
You should check the example from this documentation.
Or the example from this tutorial.
We can declare all properties and function of ProductComponent component to App component.
Then passes those to ProductComponent via props. Instead of using
<Route path="/product" exact component={ProductComponent}/>
We should use
<Route
path="/product"
render={
props => <ProductComponent products = {this.state.products}/>
}
/>
Since ProductComponent and NavBar component are child of App component we can easily passes all data of ProductComponent to NavBar component via props.
Related
I have a top App component and child LoginForm component. App component send this.history to loginform props and loginForm call history.pust('/') on successful login
But if I need to call this.history in App constructor or in comonentDidMount it is undefined. I dont understand ho is it possible. Can somebody help me please to understand it?
import React from "react"
import {
Switch,
Route,
NavLink
} from "react-router-dom"
import Home from "./components/Home"
import Topics from "./components/Topics"
import Login from "./components/Login"
import About from "./components/About"
import 'bootstrap/dist/css/bootstrap.css'
import './assets/App.scss';
import { withRouter } from "react-router-dom"
import axios from 'axios'
window.$ = require('jquery')
require('popper.js')
require('bootstrap')
class App extends React.Component
{
constructor(props)
{
super(props)
this.state = {
userName: '',
accessToken: this.getAccessToken()
}
if( ! this.state.accessToken ) this.history.push('/login') // Error call push of undefined
}
componentDidMount()
{
console.log(this.history) // undefined <<<<<<<<<<<<<<<
}
render()
{
return (
<div>
<nav className="navbar navbar-dark bg-dark">
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item">
<NavLink to="/" className="nav-link">Home</NavLink>
</li>
<li className="nav-item">
<NavLink to="/login" className="nav-link">Login</NavLink>
</li>
</ul>
</div>
</nav>
<div className="container-fluid content">
<div>
<Switch>
<Route path="/" exact>
<Home />
</Route>
<Route path="/login">
<Login history={this.history} /> // This is ok <<<<<<<<<<<<<<
</Route>
</Switch>
</div>
</div>
</div>
);
}
}
export default App
! [My Output is shown like this. ]1. I created one react project, and for designing I am using Bootstrap framework. I am done routing in my project. Routing is working fine, But Navbar is not working properly in my react project. So help me to resolve this issue.
This is App.js
import React from 'react';
import './App.css';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import Home from './Pages/Home/Home';
import Edit from './Pages/Edit/Edit';
import Create from './Pages/Create/Create';
import Navbar from './Components/Navbar/Navbar';
function App() {
return (
<Router>
<Navbar></Navbar>
<div className="container">
<h2>MERN</h2>
<Route path='/' exact component={Home}></Route>
<Route path='/id:/edit' component={Edit}></Route>
<Route path='/create' component={Create}></Route>
</div>
</Router>
);
}
export default App;
In Components Folder, I have Navbar folder, I Navbar folder I have Navbar.js
In Navbar.js
import React, { Component } from "react";
import "./Navbar.css";
import { Link } from "react-router-dom";
export default class Navbar extends Component {
render() {
return (
<div>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="www.facebook.com">
Navbar
</a>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<li className="nav-item active">
<Link to="/">Home</Link>
</li>
<li className="nav-item">
<Link to="/create">Create</Link>
</li>
<li className="nav-item">
<Link to="/id:/edit">Edit</Link>
</li>
</ul>
</div>
</nav>
</div>
);
}
}
In Pages Folder I have three folders 1)Home 2)Create 3)Edit
This is Home.js
import React, { Component } from "react";
import "./Home.css";
export default class Home extends Component {
render() {
return (
<div>
<p>Welcome to Home Component</p>
</div>
);
}
}
This is Create.js
import React, { Component } from "react";
export default class Create extends Component {
render() {
return (
<div>
<p>Welcome to Create Component</p>
</div>
);
}
}
This is Edit.js
import React, { Component } from "react";
export default class Edit extends Component {
render() {
return (
<div>
<p>Welcome to Edit Component</p>
</div>
);
}
}
In my react project Routing is working fine, but Navbar is not working properly. If I am not clear with my doubt please put a comment.
Actually you just forgot to add the necessary className to your Links.
Add className="nav-link" to Links in Navbar.js.
<ul className="navbar-nav">
<li className="nav-item active">
<Link to="/" className="nav-link">
Home
</Link>
</li>
<li className="nav-item">
<Link to="/create" className="nav-link">
Create
</Link>
</li>
<li className="nav-item">
<Link to="/id:/edit" className="nav-link">
Edit
</Link>
</li>
</ul>
Have you tried adding the Switch component around all of your <Route> components?
You can find a similar example to what you are trying to achieve here in the first basic example.
Try:
<Router>
<Navbar></Navbar>
<div className="container">
<h2>MERN</h2>
<Switch>
<Route path='/' exact component={Home}></Route>
<Route path='/id:/edit' component={Edit}></Route>
<Route path='/create' component={Create}></Route>
</Switch>
</div>
</Router>
I am assuming by not-working properly, the "active" class is not changing.
For this you can use "NavLink" instead of "Link" from the react-router-dom package and do not hardcode the "active" class to the <li>
import { NavLink } from 'react-router-dom';
...
<li className="nav-item">
<NavLink to='/' activeClassName="active">Home</NavLink>
</li>
<li className="nav-item">
<NavLink to='/create' activeClassName="active">Create</NavLink>
</li>
<li className="nav-item">
<NavLink to='/id:/edit' activeClassName="active">Edit</NavLink>
</li>
...
error screenshotI am trying to include React-router in my React app , and i am using it in the navigation component, but after using it and initializing the path , my react app is crashing . Please refer code below :
Have tried writing according t react-router v4 syntax , the is used in the App.js file and the Link and Route have been used in Navigation.js component .
import React,{ Component } from 'react';
import { Route, Link } from "react-router-dom";
import './Navigation.css';
import App from '../../App';
import Create from '../pages/create/Create';
import List from '../pages/list/List';
class Navigation extends Component{
render(){
return (
<div>
<nav className="navbar navbar-expand-md navbar-dark bg-dark">
<Link className="navbar-brand mr-auto" to="/">HOME</Link>
<div id="navbarSupportedContent">
<ul className="navbar-nav">
<li className="nav-item active">
<Link className="nav-link" to="/Create">Create</Link>
</li>
<li className="nav-item" >
<Link className="nav-link" to="/List">List</Link>
</li>
</ul>
</div>
</nav>
{/* <Route path="/" exact component={App} /> */}
<Route path="/Create" component={Create} />
<Route path="/List" component={List} />
</div>
);
}
}
export default Navigation;
App.js:
import React, { Component } from 'react';
import Navigation from './js/navigation/Navigation.js';
import { BrowserRouter as Router } from 'react-router-dom';
class App extends Component {
render() {
return (
<Router>
<div className="App">
<Navigation/>
<div class="jumbotron text-center">
<h1 class="display-4">Welcome</h1>
<hr class="my-4"/>
<p>Please Click on Create to Create the Schema and on List to View the Schema.</p>
</div>
</div>
</Router>
);
}
}
export default App;
when i run the app and click on Create or List , respective components should be rendered but app is crashing when i run the react-app.
According to your crash screenshot, Your app is entering an infinite loop in your Create Component, please check your create component code
I'm using react-router v4 for routing in React. The issues i'm facing is that the react-router changes the location but is unable to render the component. I heard that using redux with react blocks updates but i'm not using redux here .
Sidebar.js
import React, {Component} from 'react';
import {
BrowserRouter as Router,
Link
} from 'react-router-dom';
import $ from 'jquery';
import {withRouter} from 'react-router-dom';
class Sidebar extends Component {
render () {
return (
<Router>
<div>
<button className="navbar-toggler" data-toggle="collapse" data-target="#pill">
<span><i className="fa fa-ellipsis-v"></i> menu</span>
</button>
<div id="pill">
<ul className="nav nav-pills flex-column sidebar">
<li className="nav-item">
<Link to="/" className="nav-link">
<i className="fa fa-dashboard"></i> Dashboard
</Link>
</li>
<li className="nav-item">
<a href="#students" className="nav-link" data-toggle="collapse">
<i className="fa fa-users"></i> Students
</a>
<ul className="collapse" id="students" style={{marginLeft: '-25px'}}>
<li className="nav-item"><Link to="/students" className="nav-link"><i className="fa fa-eye"></i> View students</Link></li>
</ul>
</li>
</ul>
</div>
</div>
</Router>
)
}
}
export default withRouter(Sidebar);
App.js
import React, { Component } from 'react';
import Navbar from './Navbar';
import Sidebar from './Sidebar';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import Dashboard from './Dashboard';
import Students from './Students';
class App extends Component {
render() {
const { match } = this.props;
return (
<Router>
<div>
<Navbar />
<div className="container-fluid main">
<div className="row">
<div className="col-sm-2 area-left">
<Sidebar />
</div>
<div className="col-sm-10 area-right float-right">
<div>
<Route exact path="/" component={Dashboard} />
<Route exact path="/students" component={Students} />
</div>
</div>
</div>
</div>
</div>
</Router>
);
}
}
export default App;
When route is /
When route is /students
Reloading the page renders the component
The Link you specified in your sidebar is trying to use the Router you wrapped the component in, not the Router you defined your links in.
This is what your sidebar component should look like.
import React, {Component} from 'react';
import {
Link,
withRouter
} from 'react-router-dom';
import $ from 'jquery';
class Sidebar extends Component {
render () {
return (
<div>
<div>
<button className="navbar-toggler" data-toggle="collapse" data-target="#pill">
<span><i className="fa fa-ellipsis-v"></i> menu</span>
</button>
<div id="pill">
<ul className="nav nav-pills flex-column sidebar">
<li className="nav-item">
<Link to="/" className="nav-link">
<i className="fa fa-dashboard"></i> Dashboard
</Link>
</li>
<li className="nav-item">
<a href="#students" className="nav-link" data-toggle="collapse">
<i className="fa fa-users"></i> Students
</a>
<ul className="collapse" id="students" style={{marginLeft: '-25px'}}>
<li className="nav-item"><Link to="/students" className="nav-link"><i className="fa fa-eye"></i> View students</Link></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
)
}
}
export default withRouter(Sidebar);
More explanation:
You should only wrap your top level component in the router since it for lack of better terms spawns all the children in your app. Furthermore, it will automatically inject the props for the router unless you are using redux. So unless you're using redux no need for withRouter() *.
*lmk if i'm wrong, i'm not an expert.
Example of your top level app component:
render() {
return(
<Router history={History}>
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/student" component={Student} />
</Switch>
</Router>
)
}
Essentially wrap your Route with Switch.
Update App.js to the below
import React, { Component } from 'react';
import Navbar from './Navbar';
import Sidebar from './Sidebar';
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Dashboard from './Dashboard';
import Students from './Students';
class App extends Component {
render() {
const { match } = this.props;
return (
<Router>
<div>
<Navbar />
<div className="container-fluid main">
<div className="row">
<div className="col-sm-2 area-left">
<Sidebar />
</div>
<div className="col-sm-10 area-right float-right">
<Switch>
<Route exact path="/" component={Dashboard} />
<Route exact path="/students" component={Students} />
</Switch>
</div>
</div>
</div>
</div>
</Router>
);
}
}
export default App;
My website is split into two component, a "Side" component that displays a nav menu and a "View" with a "Menu" component that display the a specific component depends on nav button you click on.
The problem is that the component never rendering.
My Menu Component :
import React from 'react';
import { BrowserRouter as Router, Link, BrowserRouter } from 'react-
router-dom';
import './Menu.css'
class Menu extends React.Component{
render(){
return (
<div className="Menu col 12" >
<div className="Menu-link" >
<Router>
<ul class="pagination">
<li class="waves-effect"><i class="material-icons">chevron_left</i></li>
<li class="waves-effect menu-element">
<Link to="/work" className="nav-title">
<i class="material-icons">equalizer</i>
</Link>
</li>
<li class="waves-effect menu-element">
<Link to="/work" className="nav-title">
<i class="material-icons">work</i>
</Link>
</li>
<li class="waves-effect menu-element">
<Link to="/education" className="nav-title">
<i class="material-icons">library_books</i>
</Link>
</li>
{/* <li class="waves-effect menu-element"><i class="material-icons">equalizer</i></li> */}
<li class="waves-effect"><i class="material-icons">chevron_right</i></li>
</ul>
</Router>
</div>
</div>
)
}
}
export default Menu;
My Side component :
import React from 'react';
import { BrowserRouter as Router, Route, Link, Switch } from 'react-
router-dom'
import './View.css'
import Work from '../Work/Work'
import Skills from '../Skills/Skills'
import Education from '../Education/Education'
class View extends React.Component{
render(){
return (
<Router>
<div>
<Switch>
<Route path='/work' component={Work} />
<Route path='/skills' component={Skills} />
<Route path='/education' component={Education} />
</Switch>
</div>
</Router>
)
}
}
export default View;
Do you have a solution to solve this ?
So <Route/> and <Link/> components need to be children of the same <Router/> component. Move your <Router/> component up into a common ancestor of the both the view and sidebar components and remove the <Router /> component from both the view and sidebar components.
If you're using redux you will need to wrap the component that has your <Route/> components with the withRouter HoC.
Edit: Here is a example code sandbox to show you how this works. Click on the about and home links on the side to see the components change.