Installations
npm install react-stack-nav
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
7.0.0
NPM Version
3.10.2
Score
69.7
Supply Chain
98.4
Quality
79
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
tuckerconnelly
Download Statistics
Total Downloads
3,347
Last Day
1
Last Week
15
Last Month
41
Last Year
349
GitHub Statistics
117 Stars
34 Commits
10 Forks
7 Watching
2 Branches
1 Contributors
Bundle Size
13.83 kB
Minified
4.87 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.4
Package Id
react-stack-nav@0.1.4
Size
10.46 kB
NPM Version
3.10.2
Node Version
7.0.0
Total Downloads
Cumulative downloads
Total Downloads
3,347
Last day
0%
1
Compared to previous day
Last week
150%
15
Compared to previous week
Last month
127.8%
41
Compared to previous month
Last year
-8.6%
349
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
React stack nav
Simple universal navigation for React Native and React
- Universal Works in iOS, Android, and Web
- Familiar API Has the same API as the web's History API
- Back/forward button support Automatic support for Android back button and back/forward buttons on web
- Deep linking support Automatic support for deep linking
- Server-side rendering Simple as pre-loading redux state with the requested url
- Composable and declarative Uses React's component tree to compose and handle routes
- Use any navigation paradigm Abstract enough to let you build the navigation with any UI components you want
- Easy to understand You can read the source! Only ~300 lines of code
Examples
iOS | Android | Web |
---|---|---|
Drawer example | ||
![]() | ![]() | ![]() |
Bottom nav with stacks example | ||
![]() | ![]() | ![]() |
The examples repo is over here.
You can also check out a real-world example in the Carbon UI Docs source.
What it looks like
A navigation component (drawer, tabs, anything):
1import React from 'react' 2import { Button, View } from 'react-native' 3import { connect } from 'react-redux' 4import { pushState } from 'react-stack-nav' 5 6const Navigation = ({ pushState }) => 7 <View> 8 <Button onPress={() => pushState(0, 0, '')}>Go to home</Button> 9 <Button onPress={() => pushState(0, 0, '/myRoute')}>Go to My Route</Button> 10 </View> 11 12const mapDispatchToProps = { pushState } 13 14export default connect(null, mapDispatchToProps)(Navigation)
A component receives and handles the first fragment of the url:
1import React from 'react' 2import { Text, View } from 'react-native' 3import { createOrchestrator } from 'react-stack-nav' 4 5const Index = ({ routeFragment }) => 6 <View> 7 {routeFragment === '' && <Text>Home</Text>} 8 {routeFragment === 'myRoute' && <Text>My route</Text>} 9 </View> 10 11export default createOrchestrator()(Index)
Installation
npm -S i react-stack-nav
Then match your redux setup to this:
1import { createStore, applyMiddleware, compose, combineReducers } from 'redux' 2import thunk from 'redux-thunk' 3import { navigation, attachHistoryModifiers } from 'react-stack-nav' 4import { BackAndroid, Linking } from 'react-native' 5 6import app from './modules/duck' 7 8const rootReducer = combineReducers({ navigation }) 9 10export default (initialState = {}) => 11 createStore( 12 rootReducer, 13 initialState, 14 compose( 15 applyMiddleware(thunk), 16 attachHistoryModifiers({ BackAndroid, Linking }), 17 ), 18 )
If you want deep linking to work, make sure you set it up per instructions here.
Principles
- Treat the redux store as a single-source-of-truth for routing
- Use URLs and the History API, even in React Native
- Treat the URL as a stack, and "pop" off fragments of the URL to orchestrate transitions between URLs
- Leave you in control, favor patterns over frameworks
Usage
At the core of react-stack-nav is the navigation
reducer, whose state looks like this:
1{ 2 index: 0, // The index of the current history object 3 history: [{ statObj, title, url }], // An ordered list of history objects from pushState 4}
If you want to navigate to a new url/page, use the pushState
action creator in one of your components (it's the same as history.pushState!):
1import { pushState } from 'react-stack-nav' 2 3class MyComponent { 4 _handleClick = () => this.props.pushState({ yo: 'dawg' }, 'New Route', '/newRoute') 5}
That'll push a new object onto state.navigation.history
, so your new navigation reducer state might look like this:
1{ 2 index: 1, 3 history: [ 4 { stateObj: {}, title: '', url: '' }, 5 { stateObj: {}, title: 'New Route', '/newRoute' } 6 ] 7}
Now say you clicked back in the browser (or hit the android back button), your state would automatically update to:
1{ 2 index: 0, 3 history: [/* same as above */] 4}
With index: 0
to say, hey, we're on the first history entry.
If you want to use the history object, to ya know, render stuff, you can do so directly:
1 2const MyComponent = ({ url, title }) => 3 <View> 4 <Text>{title}</Text> 5 {url === '/users' && <UsersPage />} 6 {url === '/pictures' && <PicturesPage />} 7 </View> 8 9const mapStateToProps = ({ navigation }) => ({ 10 url: navigation.history[navigation.index].url, 11 title: navigation.history[navigation.index].url, 12}) 13 14connect(mapStateToProps)(MyComponent)
You could handle all your routing like that, but...wait a minute...if you turn a url sideways, it kinda looks like a stack yeah?
/users/1/profile
users
-------
1
-------
profile
react-stack-nav
runs with this idea and introduces the idea of an orchestrator. Orchestrators pops off an element in the stack and declaratively handles transitions when it changes:
users -------> Popped off and handled by first orchestrator in component tree
-------
1 -------> Popped off and handled by second orchestrator in component tree
-------
profile -------> Popped off and handled by third orchestrator in component tree
The first orchestrator found in the component tree would handle changes to the top of the stack:
1import { createOrchestrator } from 'react-stack-nav' 2 3const Index = ({ routeFragment }) => { 4 <View> 5 {routeFragment === 'users' && <UsersIndex />} 6 {routeFragment === 'pictures' && <PicturesIndex />} 7 </View> 8} 9 10export default createOrchestrator()(Index)
The next orchestrators found in the tree would handle the next layer of the url stack, so UsersIndex
might look like this:
1class UsersIndex extends Component { 2 async componentWillReceiveProps(next) { 3 const { routeFragment } = this.props 4 if (routeFragment !== next.routeFragment) return 5 6 this.setState({ user: await fetchUserData(routeFragment) }) 7 } 8 9 render() { 10 <View> 11 {/* Use this.state.user info */} 12 </View> 13 } 14} 15 16export default createOrchestrator('users')(UsersIndex)
createOrchestrator()
accepts a string or a regular expression for that particular layer of that stack. If it doesn't match the current layer of the url, props.routeFragment
will be undefined.
You could create orchestrators ad inifitum all the way down the component tree to handle as much of the URL stack as you want:
1const UserIndex = ({ routeFragment }) => { 2 <View> 3 {routeFragment === 'profile' && <ProfileIndex />} 4 {routeFragment === 'settings' && <SettingsIndex />} 5 </View> 6} 7 8export default createOrchestrator(/\d+/)(UserIndex)
1const ProfileIndex = ({ routeFragment }) => { 2 <View> 3 {/* Would handle /users/${userId}/profile/posts */} 4 {routeFragment === 'posts' && <UserPosts />} 5 {/* Would handle /users/${userId}/profile/pictures */} 6 {routeFragment === 'pictures' && <UserPictures />} 7 </View> 8} 9 10export default createOrchestrator('profile')(ProfileIndex) 11
API
attachHistoryModifiers
- Store enhancer for redux, handles android back-button presses, browser back/forward buttons, and deep linkingcreateOrchestrator(fragment: string | regexp)
-- Higher-order-component that creates an orchestratornavigation
--navigation
reducer for setting up your store
Action creators
These are the same as the History API :
pushState(stateObj: object, title: string, url: string)
: Pushes a new state onto the history arrayreplaceState(stateObj: object, title: string, url: string)
: Replaces the current state on the history arrayback(reduxOnly: bool)
: Moves thenavigation.index
back one. IfreduxOnly
is true, it won't change the browserhistory
state (this is mostly for internal use)forward(reduxOnly: bool)
: Moves thenavigation.index
forward one, reduxOnly is the same as forback()
go(numberOfEntries: int)
: Moves thenavigation.index
forward/backward by the passed number (go(1)
is the same asforward()
, for example).replaceTop(stateObj: object, title: string, toUrl: string)
: LikereplaceState
, but appends thetoUrl
to the current url instead of replacing it outright. Primarily used for index redirects.pushTop(stateObj: object, title: string, toUrl: string)
: LikepushState
, but appends thetoUrl
to the current url instead of replacing it outright. Primarily used for card stacks.
Connect
Follow the creator on Twitter, @TuckerConnelly
License
MIT
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More