Giter Club home page Giter Club logo

react-passage's Introduction


npm version

Passage helps when linking or redirecting to routes that may or may not be in your react app.

The idea is simple: Wrap Passage around your routes so it knows what routes have been defined in your app. Then, using the Link and Redirect components from Passage will honor the HTML5 history API if the route is within your app, otherwise falling back to other means such as anchor tags or location redirects.

Note: There may be some issues with nested react routes. Read more here.

Installing

Install via NPM:

npm i @dollarshaveclub/react-passage@latest --save

Usage

Passage provides three exports.

  • A Passage component used for identifying routes in your app
  • A Link component, use this to render react links internal/external routes
  • A Redirect component, use this to redirect to internal/external routes

Wrap the Passage component around your routes

import React from 'react'
import { BrowserRouter, Route, Switch } from 'react-router-dom'

import { Passage } from '@dollarshaveclub/react-passage'

const App = () => (
  <Passage>
    <BrowserRouter>
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/topics" component={Topics} />
      </Switch>
    </BrowserRouter>
  </Passage>
)

The Passage component accepts an optional prop called targets. This is an array of components that you want to search for within your routes file. It has a value of [Route] by default.

const App = () => (
  <Passage targets={[ Route, MyCustomRoute ]}>
    <BrowserRouter>
      <Switch>
        <MyCustomRoute exact path="/" component={Home} />
        <MyCustomRoute path="/about" component={About} />
        <Route path="/topics" component={Topics} />
      </Switch>
    </BrowserRouter>
  </Passage>
)

Leverage Passage Links and Redirects

import React from 'react'

import {
  Link,
  Redirect,
} from '@dollarshaveclub/react-passage'

// Renders a React Router Link tag if it can, otherwise falls back to an anchor tag
const aboutExample = () => (<Link to='/about'>About</Link>)

// Force Link to render an anchor tag
const externalExample = () => (<Link external to='https://www.google.com'>Google</Link>)

// Redirects with react-history if route exists, otherwise, uses window.location.assign
const externalExample = () => (<Redirect to='/external-path' />)

// Change how you redirect
const changeRedirectExample = () => (
  <Redirect to='/new-website' via={(to) => window.location.href = to} />
)

License

MIT

react-passage's People

Contributors

bensonnn avatar doctyper avatar greenkeeper[bot] avatar jakiestfu avatar kevinpeno avatar kylecorbelli avatar yowainwright avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-passage's Issues

An in-range update of @types/react is breaking the build 🚨

The devDependency @types/react was updated from 16.7.22 to 16.8.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/react is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: build: Your tests failed on CircleCI (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Not installable from public NPM?

yarn add @dollarshaveclub/react-passage
yarn add v1.12.1
[1/4] 🔍  Resolving packages...
error An unexpected error occurred: "https://registry.npmjs.org/@dollarshaveclub%2freact-passage: Not found".

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Improve Test Coverage For `to` as object & function

Requested Update

Enhance test.js - describe('react-passage', () => {})

Provide consistent coverage for:

  • to string
  • to as location object - and pathname, search, hash, state
  • to as function and pathname, search, hash, state

Why Is This Update Needed?

The to property of React Passage's Link accepts a string, object or function which is higher complexity than just working with a url string.

Currently test.js tests are light, and do an incomplete job ensuring that Passage's Link are working correctly by retain all the relevant values pathname, search, hash, state.

Are There Examples?

One example of light coverage:
it('matches with a function that returns a location object', () => {})
The incompleteness is missing coverage to ensure state is passed through.

Bug in Tests: expectComponentToRenderSafeLink()

Requested Update

Fix and rename expectComponentToRenderSafeLink(). This jest expectation helper doesn't work as expected. Example:

 * expectComponentToRenderSafeLink(component, 'a', '/foo') // incorrectly always passes

Why Is This Update Needed?

So the tests can be accurate, and we can verify when a react-route-dom Link from react-passage was incorrectly rendered between these two choices:

  • react-router-dom Link - data-safelink-type="link"
  • simple <a> - data-safelink-type="a"

Spike: Nested Routes

Issue: If nested routes are used, Passage will not have access to all of the routes at any given point in time.

How can Nested Routes be used with Passage? Is this possible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.