npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-navigation-guard

v1.0.5

Published

navigation guard component for React

Downloads

6

Readme

react-navigation-guard

npm license

Why React need navigation guard?

React is pretty cool. I like to make something with React, but there's something React doesn't have. Navigation-Guard, Vue has 'navigation guard' basically, it is kinda function that activated when url changed(it means, when user redirect). React also can use 'history' for navigation checking and it makes developer control component in the react-router. But, 'history' options needed to set in the each of every component and i don't wanna do that. Nevertheless, React is nice, there's no doubt. But i'm greedy. I want more.

So?

So! I made a simple navigation guard module for React. It is really simple, but you can use this to check url, url parameter and route path. If you want, also you can use this with Redux(react-redux etc...). I hope you geeks like this! Enjoy!

How to install

Install with npm.

cd to/your/npm/project
npm install --save react-navigation-guard

Before using

react-navigation-guard is a React Component and wrap the Route that imported from react-router-dom. So, you will need several react-router-dom module's object, like Switch and BrowserRouter. I will show you the example in the "how to use" part.

How to use(with example code)

I will let you know how to set this module and show you the sample code of src/App.js file of React project.
Also you can see this sample code in react-navigation-guard-sample repo.


First

You need to import everything you need.

// using ES6 modules
import NavigationGuard from 'react-navigation-guard';
// and other things
import React, { Component } from 'react';
import { BrowserRouter, Switch } from 'react-router-dom';

Second

Make Router component. Point is, you have to pass these 6 props, exaxt, component, path, returnBool, ifTrue and ifFalse, and it is not optional. First thing first, i will use 3 basic props. If you have used react-router-dom module, you will know what these props do.

exact : Bool. You can choose True or False. True will set exact option and false will not.
component : Function. Yes, react component is function. This is easy part, import or make component and pass it.
path : String. You can set a path for pages. It is kind of address to each component.

Example Code

class Router extends Component {
  render() {
    return (
      <Switch>
        <NavigationGuard exact={true} component={Home} path='/' />
        <NavigationGuard exact={true} component={Test} path='/test' />
      </Switch>
    )
  }
}

Next is other 3 props. These are react-navigation-guard's own props and you have to make your own functions for these.

NOTICE

  • You must write two or three parameters when you make functions.

returnBool : Function(url, params). This function must return bool type. You can use this like example code below.
ifTrue : Function(url, params, stopRender). This function ran when returnBool function's return value is true.
ifFalse : Function(url, params, stopRender). This function ran when returnBool function's return value is false.

url : string, NOT optional. url of component you want to move.
params : object, NOT optional. path parameters of URL.
blockRender : function, NOT optional. if you want to block rendering the component, use this parameter.

Example Code

class Router extends Component {
  returnBool = (url, params) => {
    console.log(url); // will show you like this "/sample/url"
    console.log(params); // will show you like this {some: "something", some2: "something2"}
    if(url === '/url/example') {
      return true;
    } else {
      return false;
    }
  }

  ifTrue = (url, params, blockRender) => {
    // will render the component you set after this function end.
    console.log('do something when true');
  }

  ifFalse = (url, params, blockRender) => {
    blockRender(); // will not render the component you set
    console.log('do something when false');
  }

  render() {
    return (
      <Switch>
        <NavigationGuard exact={true} component={Home} path='/' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
        <NavigationGuard exact={true} component={Test} path='/test/:test' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
      </Switch>
    )
  }
}

One more thing, you don't need to use NavigationGuard if some component is not needed to check. You can just use Route.

Example Code

  render() {
    return (
      <Switch>
        <NavigationGuard exact={true} component={Home} path='/' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
        <NavigationGuard exact={true} component={Test} path='/test/:test' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
        <Route path='/dont/need/to/check' component={DontCheck} />
      </Switch>
    )
  }

Third

Set the App component.

class App extends Component {
  render() {
    return (
      <div className="App">
        <BrowserRouter>
          <Router/>
        </BrowserRouter>
      </div>
    );
  }
}

export default App;

Fourth

Make few simple component for test. I already use these component for second part.

class Home extends Component {
  render() {
    return (
      <h1>home</h1>
    )
  }
}

class Test extends Component {
  render() {
    return (
      <h1>test</h1>
    )
  }
}

Full code of example

import React, { Component } from 'react';
import { BrowserRouter, Switch } from 'react-router-dom';
import NavigationGuard from 'react-navigation-guard';

class Home extends Component {
  render() {
    return (
      <h1>home</h1>
    )
  }
}

class Test extends Component {
  render() {
    return (
      <h1>test</h1>
    )
  }
}

class Router extends Component {
  returnBool = (url, params) => {
    console.log(url); // will show you like this "/sample/url"
    console.log(params); // will show you like this {some: "something", some2: "something2"}
    if(url === '/url/example') {
      return true;
    } else {
      return false;
    }
  }

  ifTrue = (url, params, blockRender) => {
    // will render the component you set after this function end.
    console.log('do something when true');
  }

  ifFalse = (url, params, blockRender) => {
    blockRender(); // will not render the component you set
    console.log('do something when false');
  }

  render() {
    return (
      <Switch>
        <NavigationGuard exact={true} component={Home} path='/' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
        <NavigationGuard exact={true} component={Test} path='/test/:test' returnBool={this.returnBool} ifTrue={this.ifTrue} ifFalse={this.ifFalse} />
      </Switch>
    )
  }
}

class App extends Component {
  render() {
    return (
      <div className="App">
        <BrowserRouter>
          <Router/>
        </BrowserRouter>
      </div>
    );
  }
}

export default App;

License

MIT