Coder Social home page Coder Social logo

slorber / react-navigation-hooks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from react-navigation/hooks

0.0 3.0 0.0 166 KB

React hooks for convenient react-navigation use

Home Page: https://reactnavigation.org

License: MIT License

JavaScript 100.00%

react-navigation-hooks's Introduction

React Navigation Hooks

npm version CircleCI badge PRs Welcome

๐Ÿ„โ€โ™€๏ธ Surfing the wave of React Hook hype with a few convenience hooks for @react-navigation/core v3. Destined to work on web, server, and React Native. Contributions welcome!

Examples (web only so far)

See an example web app which uses react-navigation and hooks on the client and the server:

https://github.com/react-navigation/web-server-example

Docs

yarn add react-navigation-hooks

import * from 'react-navigation-hooks'

useNavigation()

This is the main convenience hook. It provides the regular navigation prop, as you'd get via the screen prop or by using withNavigation.

You can use the navigate functionality anywhere in your app:

function MyLinkButton() {
  // be careful to never call useNavigation in the press callback. Call hooks directly from the render function!
  const { navigate } = useNavigation();
  return (
    <Button
      onPress={() => {
        navigate('Home');
      }}
      title="Go Home"
    />
  );
}

useNavigationParam(paramName)

Access a param for the current navigation state

function MyScreen() {
  const name = useNavigationParam('name');
  return <p>name is {name}</p>;
}

Literally the same as useNavigation().getParam(paramName)

useNavigationState()

Access the navigation state of the current route, or if you're in a navigator view, access the navigation state of your sub-tree.

function MyScreen() {
  const { routeName } = useNavigationState();
  return <p>My route name is {routeName}</p>;
}

Literally the same as useNavigation().state

useNavigationKey()

Convenient way to access the key of the current route.

Literally the same as useNavigationState().key

useNavigationEvents(handler)

Subscribe to navigation events in the current route context.

function ReportNavigationEvents() {
  const [events, setEvents] = useState([]);
  useNavigationEvents(evt => {
    // latest state on evt.state
    // prev state on evt.lastState
    // triggering navigation action on evt.action

    setEvents(events => [...events, evt.type]);
    // evt.type is [will/did][Focus/Blur]
  });
  return (
    <>
      {events.map(evt => (
        <p>{evt.type}</p>
      ))}
    </>
  );
}

The event payload will be the same as provided by addListener, as documented here: https://reactnavigation.org/docs/en/navigation-prop.html#addlistener-subscribe-to-updates-to-navigation-lifecycle

useFocusState()

Convenient way of subscribing to events and observing focus state of the current screen.

function MyFocusTag() {
  return <p>{useFocusState().isFocused ? 'Focused' : 'Not Focused'}</p>;
}

One (always, and only one) of the following values will be true in the focus state:

  • isFocused
  • isBlurring
  • isBlurred
  • isFocusing

react-navigation-hooks's People

Contributors

ericvicenti avatar slorber avatar nem035 avatar

Watchers

 avatar James Cloos avatar  avatar

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.