I simply try to use react provider but somehow i get this error:
index.js:1437 Warning: A context consumer was rendered with multiple children, or a child that isn't a function. A context consumer expects a single child that is a function. If you did pass a function, make sure there is no trailing or leading whitespace around it.
this is context.js
import React, { Component } from 'react'
const Context=React.createContext()
class Providerr extends Component {
state={display:false}
displayeditor=()=>{this.setState({display:!this.state.display}) }
render() { return (
<Context.Provider value={{...this.state,displayeditor:this.displayeditor }}>
{this.props.children}
</Context.Provider>
)
}
}
const Consumer=Context.Consumer
export {Providerr,Consumer}
I want to Consume it below
import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {
render() {
return (
<nav className="navbar navbar-expand-lg">
<a className="navbar-brand" href="#Home">
<img src="./images/customLogo.jpg" className="navlogo"/>
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav mx-5">
<NavLink to="/">
<li className="nav-item active mr-5">
<div className="nav-link">
<span>Home</span>
</div>
</li>
</NavLink>
<Consumer> {value =>
<li onClick={value.displayeditor} className="nav-item mr-5">
<div className="nav-link modal_lig" href="">
<span>Leagues</span>
</div>
</li> }
</Consumer>
</nav>
)}}
Wrap the whole code inside Consumer as shown below. Also note that some of your html elements are not properly closed.
import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {
render(){
return(
<Consumer>
{value => (
<nav className="navbar navbar-expand-lg">
<a className="navbar-brand" href="#Home">
<img src="./images/customLogo.jpg" className="navlogo"/>
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav mx-5">
<NavLink to="/">
<li className="nav-item active mr-5">
<div className="nav-link">
<span>Home</span>
</div>
</li>
</NavLink>
<li onClick={()=>value.displayeditor()} className="nav-item mr-5">
<div className="nav-link modal_lig" href="">
<span>Leagues</span>
</div>
</li>
</ul>
</div>
</nav>
)}
</Consumer>
)
}
}
Related
I have seen multiple questions related to this and I have tried them all, with no results. So, posting yet another seemingly duplicate question.
I am trying to highlight the current page button in the navigation bar. For simple examples, where I am not routing it to a new page, it works. But when I route it to a different page (a separate react component) it doesn't work.
Here is the code I have:
App.jsx:
class App extends Component {
render() {
return (
<BrowserRouter>
<div>
<Route exact={true} path='/' component={HomeApp}/>
<Route path='/form' component={SomeForm}/>
</div>
</BrowserRouter>
);
}
}
NavigationBar.jsx
class NavigationBar extends Component {
componentDidMount() {
toggleIcon();
}
render() {
return (<div id="topheader">
<nav className="navbar navbar-expand-lg navbar-light bg-light ">
<a className="navbar-brand" href="#">Whatever</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 nav nav-pills ml-auto">
<li className="nav-item active">
<a className="nav-link" href="/">Home <span className="sr-only">(current)</span></a>
</li>
<li className="nav-item">
<a className="nav-link" href="/form">Submit Form</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Sign Up</a>
</li>
<li className="nav-item">
<a className="nav-link " href="#">Login</a>
</li>
</ul>
</div>
</nav>
</div>
)
}
}
export default NavigationBar
navigation.js
import $ from 'jquery';
export function toggleIcon() {
$( '#topheader .navbar-nav a' ).on( 'click', function () {
$( '#topheader .navbar-nav' ).find( 'li.active' ).removeClass( 'active' );
$( this ).parent( 'li' ).addClass( 'active' );
});
}
As seen, the highlighting works when I click Sign Up or Login, as they are not being routed to any other component.
But when I click on Submit Form which is routed to SomeForm component, the highlighting goes back to Home button.
For more details, I am posting the contents of HomeApp and SomeForm components:
HomeApp.jsx
class HomeApp extends Component {
render() {
return (
<div className="container">
<NavigationBar/>
<Jumbotron/>
</div>
)
}
}
export default HomeApp
SomeForm.jsx
class SomeForm extends Component {
render() {
return (<>
<div className="container">
<NavigationBar>
</NavigationBar>
</div>
</>
)
}
}
export default SomeForm
Please follow along this structure:
import { BrowserRouter, Switch, Route } from "react-router-dom"
class App extends React.Component{
render() {
return (
<BrowserRouter>
<div className="App">
<Navigation />
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
<Route exact path="/contact" component={MoreRoutes} />
</Switch>
</div>
</BrowserRouter>
)
}
}
And you need to use NavLink as #Mikail Bayram mentioned, that will allow you to use activeClassName also, the exact prop is required on each of your routes to have routes render only when the path is have the exact match. Please note that you can call the active class whatever you like.
Your Navigation should look like this:
import { NavLink } from "react-router-dom"
const Navigation = () => (
<nav>
<ul>
<li>
<NavLink exact activeClassName="active" to="/">
Home
</NavLink>
</li>
<li>
<NavLink exact activeClassName="active" to="/about">
About
</NavLink>
</li>
<li>
<NavLink exact activeClassName="active" to="/contact">
Contact
</NavLink>
</li>
</ul>
</nav>
)
As a side note: You should Never combining jQuery with React
Here is a live example at codeSandbox
Since you need to highlight parent element of link (li) you can use withRouter (or useLocation hook for functional components) with NavLink from react-router in NavigationBar:
import React, { Component } from 'react';
import { withRouter } from 'react-router';
class NavigationBar extends Component {
constructor(props) {
super(props);
this.renderMenuLink = this.renderMenuLink.bind(this);
}
componentDidMount() {
toggleIcon();
}
renderMenuLink({ path, label }) {
const { location } = this.props;
const isActive = location.pathname === path;
return (
<li key={path} className={`nav-item ${isActive ? 'active' : ''}`}>
<NavLink className="nav-link" to={path}>
{label}{isActive ? <span className="sr-only">(current)</span> : ''}
</NavLink>
</li>
);
}
render() {
const menuLinks = [
{ path: '/', label: 'Home' },
{ path: '/form', label: 'Submit Form' },
{ path: '/register', label: 'Sign Up' },
{ path: '/login', label: 'Login' },
];
return (<div id="topheader">
<nav className="navbar navbar-expand-lg navbar-light bg-light ">
<a className="navbar-brand" href="#">Whatever</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 nav nav-pills ml-auto">
{menuLinks.map((menuLink) => this.renderMenuLink(menuLink))}
</ul>
</div>
</nav>
</div>
)
}
}
export default withRouter(NavigationBar);
I want to for example only show the logout button if a user is actually logged in.
My _app.js file:
import React from "react";
import App from "next/app";
import Navbar from "../components/navbar";
import Layout from "../components/layouts/mainLayout";
import { handleAuthSSR } from "../utils/auth";
export default class MyApp extends App {
render() {
const { Component, pageProps, userAuth } = this.props;
return (
<Layout userAuth={pageProps.token}>
<Component {...pageProps} />
</Layout>
);
}
}
Main Layout:
import Head from "next/head";
import Navbar from "../navbar";
const Layout = props => {
return (
<>
<Head>
<title>My App</title>
<link
rel="stylesheet"
href="https://bootswatch.com/4/cerulean/bootstrap.min.css"
></link>
<link href="/static/css/styles.css" rel="stylesheet"></link>
</Head>
<Navbar />
<div className="container">{props.children}</div>
</>
);
};
export default Layout;
Navbar:
import Link from "next/link";
import { logout } from "../utils/auth";
const Navbar = props => (
<nav className="navbar navbar-expand navbar-dark bg-dark mb-4">
<div className="container">
<a className="navbar-brand" href="#">
My App
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link href="/">
<a className="nav-link">Home</a>
</Link>
</li>
<li className="nav-item">
<Link href="/about">
<a className="nav-link">About</a>
</Link>
</li>
<li className="nav-item">
<Link href="/register">
<a className="nav-link">Register</a>
</Link>
</li>
{props.userAuth ? (
<li>
<button className="btn btn-primary" onClick={logout}>
Logout
</button>
</li>
) : (
<li className="nav-item">
<Link href="/login">
<a className="nav-link">Login</a>
</Link>
</li>
)}
</ul>
</div>
</div>
</nav>
);
export default Navbar;
So, I am passing userAuth via props but I haven't actually got the logic for userAuth.
The handleAuthSSR looks like:
export const handleAuthSSR = ctx => {
const { token } = nextCookie(ctx);
if (ctx.req && !token) {
ctx.res.writeHead(302, { Location: "/login" });
ctx.res.end();
return;
}
if (!token) {
console.log("no token found");
Router.push("/login");
}
return token;
};
I am using this as part of a HOC when a user navigates around and it works but not sure how to get this to work with regards to the navigation show/hide.
I can see that you didn't pass the userAuth prop to the navbar component in mainLayout.js. Also you need to work on your Parent/Children props communications.
Maybe you can use your HOC to return token or false (or null instead of false). Use it then in navbar.js to toggle Login/Logout link.
In my application, I have written a function under a functional component and it doesn't work. It says, function is not defined no-undef. But I have written same kind of code in my previous project. What am I doing wrong here? If I convert this to a class component, this works fine. Is it possible to write a code like this? Or do I have to change it to a class component?
import React from 'react';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import {bindActionCreators} from 'redux';
import { connect } from 'react-redux';
import { logoutUser, clearCurrentProfile } from '../../actions';
const Navbar = props => {
const { isAuthenticated, user } = props.auth;
onLogoutClick = e => {
e.preventDefault();
props.clearCurrentProfile();
props.logoutUser();
}
const authLinks = (
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link className="nav-link" to="/feed">
Post Feed
</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/dashboard">
Dashboard
</Link>
</li>
<li className="nav-item">
<img
className="rounded-circle"
style={{ width: '25px' }}
src={user.avatar}
alt={user.name}
/>
<button
type="button"
className="link-button nav-link"
onClick={() => this.onLogoutClick().bind(this)}>
Logout
</button>
</li>
</ul>
);
const guestLinks = (
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link className="nav-link" to="/register">
Sign Up
</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/login">
Login
</Link>
</li>
</ul>
);
return (
<nav className="navbar navbar-expand-sm navbar-dark bg-dark mb-4">
<div className="container">
<Link className="navbar-brand" to="/">
DevConnector
</Link>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#mobile-nav">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="mobile-nav">
<ul className="navbar-nav mr-auto">
<li className="nav-item">
<Link className="nav-link" to="/profiles">
Developers
</Link>
</li>
</ul>
{isAuthenticated ? authLinks : guestLinks}
</div>
</div>
</nav>
);
};
Navbar.propTypes = {
logoutUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
auth: state.auth
});
const mapDispatchToProps = dispatch => ({
logoutUser: bindActionCreators(logoutUser, dispatch),
clearCurrentProfile: bindActionCreators(clearCurrentProfile, dispatch)
});
export default connect(mapStateToProps, mapDispatchToProps)(Navbar);
Define with const onLogoutClick, and you do not need this keyword when calling it.
bind is also unnecessary when you are already using arrow function for lexical binding.
const Navbar = props => {
// ...
const onLogoutClick = e => { // use consts or let
e.preventDefault()
props.clearCurrentProfile()
props.logoutUser()
}
const authLinks = (
// ...
<button
type="button"
className="link-button nav-link"
onClick={onLogoutClick} // no need 'this' keyword
>
...rest of your stuffs
You got some syntax error: function declaration onLogoutClick must be started with const or let and the stateless component is just a function, so you don't need this when calling it.
import React from 'react';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import {bindActionCreators} from 'redux';
import { connect } from 'react-redux';
import { logoutUser, clearCurrentProfile } from '../../actions';
const Navbar = props => {
const { isAuthenticated, user } = props.auth;
const onLogoutClick = e => {
e.preventDefault();
props.clearCurrentProfile();
props.logoutUser();
}
const authLinks = (
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link className="nav-link" to="/feed">
Post Feed
</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/dashboard">
Dashboard
</Link>
</li>
<li className="nav-item">
<img
className="rounded-circle"
style={{ width: '25px' }}
src={user.avatar}
alt={user.name}
/>
<button
type="button"
className="link-button nav-link"
onClick={onLogoutClick}>
Logout
</button>
</li>
</ul>
);
const guestLinks = (
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link className="nav-link" to="/register">
Sign Up
</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/login">
Login
</Link>
</li>
</ul>
);
return (
<nav className="navbar navbar-expand-sm navbar-dark bg-dark mb-4">
<div className="container">
<Link className="navbar-brand" to="/">
DevConnector
</Link>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#mobile-nav">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="mobile-nav">
<ul className="navbar-nav mr-auto">
<li className="nav-item">
<Link className="nav-link" to="/profiles">
Developers
</Link>
</li>
</ul>
{isAuthenticated ? authLinks : guestLinks}
</div>
</div>
</nav>
);
};
Navbar.propTypes = {
logoutUser: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
auth: state.auth
});
const mapDispatchToProps = dispatch => ({
logoutUser: bindActionCreators(logoutUser, dispatch),
clearCurrentProfile: bindActionCreators(clearCurrentProfile, dispatch)
});
export default connect(mapStateToProps, mapDispatchToProps)(Navbar);
this is not available when using functional component. It is working when using class component because this is available.
change this
onClick={() => this.onLogoutClick().bind(this)}>
to
onClick={onLogoutClick()}>
I'm having trouble with my Actions in Redux with my NextJS app.. I'd imagine it would be relatively similar to implementing Redux in any other React application but I can't for the life of me get my actions to fire off. It has been awhile since I've touched Redux so it might be something really obvious that I'm missing but I've spent several hours trying to troubleshoot it. When my window width gets to 600px I have a breakpoint that shows a hamburger icon and I want it to add the class "open" to my navbar. Here's my repo. https://github.com/nicer00ster/nicer00ster-blog
add an eventListener to handle window resize and then check the innerWidth of window to call your action.
import React from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { toggleMenu } from '../../actions'
import Sidebar from './Sidebar';
import Nicer00ster from '-!svg-react-loader?name=Nicer00ster!../../images/svg/nav/nicer00ster.svg';
import Smartphone from '-!svg-react-loader?name=Smartphone!../../images/svg/nav/smartphone.svg';
import House from '-!svg-react-loader?name=House!../../images/svg/nav/house.svg';
import Telephone from '-!svg-react-loader?name=Telephone!../../images/svg/nav/telephone.svg';
class Navbar exntends React.Component {
constructor() {
super();
this.onWindowResize = this.onWindowResize.bind(this);
}
componentDidMount() {
window.addEventListener('resize', onWindowResize);
}
onWinodwResize() {
if (window.innerWidth < 600 && !this.props.isOpen)
this.props.toggleMenu();
}
render() {
const {isOpen, toggleMenu} = this.props;
return (
<div>
<div className={ isOpen ? "navbar open" : "navbar" }>
<ul className="navbar__links">
<li className="navbar__item">
<Link to="/">
<Nicer00ster className="logo" width={200} height={100}/>
</Link>
<Sidebar />
</li>
</ul>
<ul className="navbar__navigation">
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/">
<a className="navbar__link">
<House className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Home</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/blog">
<a className="navbar__link">
<Telephone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Blog</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/work">
<a className="navbar__link">
<Smartphone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Work</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/contact">
<a className="navbar__link">
<Telephone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Contact</span>
</a>
</Link>
</li>
</ul>
</div>
<Sidebar isOpen={isOpen} />
</div>
)
}
export default connect(function(state) {
return { isOpen: state.app.open }
}, { toggleMenu })(Navbar);
I recently tried implementing a navlink like so:
import React from 'react';
import { Link, NavLink } from 'react-router-dom';
import '../css/navbar.css';
import logoWhite from '../../common/images';
const Navbar = props => {
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logoWhite} alt="Test logo" />
</Link>
<ul className="column navigation">
<li>
<NavLink to="/" exact className="nav-link" activeClassName="active">Home</NavLink>
</li>
<li>
<NavLink to="/match" className="nav-link" activeClassName="active">Partner Proposals</NavLink>
</li>
</ul>
</div>
);
};
export default Navbar;
Upon running I encountered the issue of the classname not being passed to the navlink when the route changed. Reading on articles and it led me to suspect that the shouldComponentUpdate method was being rendered false. So, I rewrote the Navbar like so:
import React from 'react';
import { Link, NavLink } from 'react-router-dom';
import '../css/navbar.css';
import logoWhite from '../../common/images';
class Navbar extends React.Component {
shouldComponentUpdate() {
console.log('Console log activated');
return true;
}
render() {
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logoWhite} alt="Test image" />
</Link>
<ul className="column navigation">
<li>
<NavLink to="/" exact activeClassName="active" className="nav-link">Home</NavLink>
</li>
<li>
<NavLink to="/match" activeClassName="active" className="nav-link">Partner Proposals</NavLink>
</li>
</ul>
</div>
);
}
}
export default Navbar;
What's strange is that the componentDidUpdate returned the value in the console (suggesting that it returned true), but the NavLink was still not passing the activeClassName
Can anyone suggest a solution to this problem?
Based on the suggestion from #mindaJalaj, I passed a pathname prop (which was rendering and re-rendering whenever there was a update to the route from the parent component), and I implemented the Navbar as follows
import React from 'react';
import PropTypes from 'prop-types';
import { Link, NavLink } from 'react-router-dom';
import { Icon } from 'semantic-ui-react';
import '../css/navbar.css';
import logo from '../../common/images/logo.png';
import navLinks from '../navLinks';
const Navbar = props => {
function renderNavLinks() {
return navLinks.map((navLink, index) => {
const key: string = `nav-link-${navLink.path}-${index}`;
const exact: boolean = navLink.exact || false;
return (
<li key={key}>
<NavLink
to={navLink.path}
exact={exact}
className="nav-link"
isActive={() => navLink.path === props.pathname}
activeClassName="active"
>
<Icon name={navLink.icon} />
<span>{navLink.title}</span>
</NavLink>
</li>
);
});
}
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logo} alt="Test logo" />
</Link>
<ul className="column navigation">{renderNavLinks()}</ul>
</div>
);
};
Navbar.propTypes = {
pathname: PropTypes.string
};
Navbar.defaultProps = {
pathname: '/'
};
export default Navbar;