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-props-mapper

v3.1.0

Published

forward navigation params as regular props

Downloads

11,424

Readme

react-navigation-props-mapper

Forwards react-navigation params to your screen component's props directly. Supports type-checking with TypeScript.

use version 3 of this package for react-navigation version 6 and newer

use version 2 of this package for react-navigation version 5

use version 1 of this package for react-navigation version 4 and lower

yarn add react-navigation-props-mapper

or

npm i react-navigation-props-mapper

Motivation

In react-navigation there were different ways to access navigation params:

  • props.navigation.state.params (from version 1)
  • props.navigation.getParam(paramName, defaultValue) (added in version 3)
  • props.route.params (the only way to read params in version 5 and later)

Example with react-navigation v6:

function SomeComponent({ route }) {
  const { params } = route;
  return (
    <View>
      <Text>Chat with {params.user.userName}</Text>
    </View>
  );
}

This works well but if you don't want your code to be tightly coupled to react-navigation (maybe because you're migrating from version 4 to 5) or if you simply want to work with navigation params the same way as with any other props, this package will help.

withForwardedNavigationParams

Use this function be able to access the navigation params passed to your screen directly from the props. Eg. instead of props.route.params.user.userName you'd write props.user.userName. The function wraps the provided component in a HOC and passes everything from props.route.params to the wrapped component.

Usage

When defining the screens for your navigator, wrap the screen component with the provided function. For example:

import { withForwardedNavigationParams } from 'react-navigation-props-mapper';

function SomeScreen(props) {
  // return something
}

export default withForwardedNavigationParams()(SomeScreen);

TypeScript

The package comes with full TS support, so you will get the same level of type checking as you would when using react-navigation alone. It exports several TS types that replace the ones exported from react-navigation. Their name is prefixed by Forwarded:

| original type | replacement type | | ---------------------- | ------------------------------- | | StackScreenProps | ForwardedStackScreenProps | | NativeStackScreenProps | ForwardedNativeStackScreenProps | | DrawerScreenProps | ForwardedDrawerScreenProps | | BottomTabScreenProps | ForwardedTabScreenProps |

ForwardedTabScreenProps should work for all types of tab navigators.

For example:

type StackParamList = {
  Profile: { userId: string };
};

type ForwardedProfileProps = ForwardedStackScreenProps<
  StackParamList,
  'Profile'
>;

const ProfileScreen = withForwardedNavigationParams<ForwardedProfileProps>()(
  function ProfileScreenWithForwardedNavParams({ userId }) {
    // userId is of type string
  }
);

See the example app for full code.

Injecting Additional Props to Your screen

This is an advanced use-case and you may not need this. Consider the deep linking guide from react-navigation. You have a chat screen that expects a userId parameter provided by deep linking:

config: {
  path: 'chat/:userId';
}

you may need to use the userId parameter to get the respective User object and do some work with it. Wouldn't it be more convenient to directly get the User object instead of just the id? withMappedNavigationParams accepts an optional parameter, of type React.ComponentType (a React component) that gets all the navigation props and the wrapped component as props. You may do some additional logic in this component and then render the wrapped component, for example:

import React, { useContext } from 'react';
import { withForwardedNavigationParams } from 'react-navigation-props-mapper';

function UserInjecter(props) {
  const userStore = useContext(UserStoreContext);

  // In this component you may do eg. a network fetch to get data needed by the screen component.
  const { WrappedComponent, userId } = props;

  const additionalProps = {};
  if (userId) {
    additionalProps.user = userStore.getUserById(userId);
  }
  return <WrappedComponent {...props} {...additionalProps} />;
}

export const ChatScreen = withForwardedNavigationParams(UserInjecter)(
  ({ user }) => {
    // return something
  }
);

That way, in your ChatScreen component, you don't have to work with user id, but directly work with the user object.

Accessing the wrapped component

The original component wrapped by withForwardedNavigationParams is available as wrappedComponent property of the created HOC. This can be useful for testing.