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-stateful-screens

v1.0.2

Published

A custom React hook for managing stateful screen transitions in applications. It abstracts a state machine to handle various screens, transitions, and shared states, making it easier to manage complex navigation logic.

Downloads

1

Readme

React Stateful Screens 💫

A custom React hook for managing stateful screen transitions in applications. It abstracts a state machine to handle various screens, transitions, and shared states, making it easier to manage complex navigation logic.

Table of Contents

Installation

# If using npm
npm install react-stateful-screens

# If using yarn
yarn add react-stateful-screens

Usage

Using react-stateful-screens is a straightforward process. Follow these detailed steps to integrate it into your application:

Step 1: Define Screen Configuration

Create a configuration object for your screens and transitions. Each screen should have a render method and can specify transitions to other screens using the on property.

Step 2: Use the useStatefulScreens Hook

Incorporate the useStatefulScreens hook into your component, passing the screen configuration as a parameter.

Step 3: Render the Current Screen

Utilize the Screen component returned by the hook to render the current screen. You can also use the goBack function to navigate to the previous screen when needed.

Here's an example of these steps in code (further examples are provided in the Examples section):

// Step 1: Define screen configuration
const config = {
  // ... (refer to the Examples section for detailed configuration)
};

// Step 2 and 3: Use the hook and render the current screen
const MyComponent = () => {
  const { goBack, Screen } = useStatefulScreens(config);
  return <Screen />;
};

API

useStatefulScreens(config)

A React hook to manage stateful screen transitions effectively.

Parameters:

  • config: An object containing the configuration for the screens and transitions.
    • initialScreen: The name of the starting screen.
    • initialState: The initial shared state or context for the screens.
    • screens: An object defining each screen, its rendering method, and allowed transitions.
    • onExit: (optional) A callback executed when reaching a terminal state.

Returns:

  • goBack: A function to navigate back to the previous screen, or null if there's no previous screen.
  • goForward: A function to navigate to the next screen based on the defined transitions.
  • reset: A function to reset the state machine to its initial state and screen.
  • Screen: A React component representing the current screen based on the state machine's state.

Example:

const { goBack, goForward, reset, Screen } = useStatefulScreens(config);

// In the component's render method
return <Screen />;
  • initialScreen: Starting screen of the state machine.
  • initialState: Initial shared state/context for the screens.
  • screens: Configuration of screens, transitions, and associated render methods.
  • onExit: Optional callback executed when the state machine reaches a terminal state.

Returns:

  • goBack: Function to navigate back to the previous screen or null if there's no previous screen.
  • Screen: React component representing the current screen based on the state machine's current state.

Examples

Explore various examples to understand how to utilize react-stateful-screens effectively.

Basic Example

const config = {
  initialScreen: 'welcome',
  initialState: { user: null },
  screens: {
    welcome: {
      render: props => <WelcomeScreen {...props} />,
      on: { login: 'dashboard' },
    },
    dashboard: {
      render: props => <DashboardScreen {...props} />,
      on: {},
    },
  },
};

const MyComponent = () => {
  const { goBack, goForward, reset, Screen } = useStatefulScreens(config);

  const handleLoginSuccess = () => {
    goForward('dashboard');
  };

  const handleLogout = () => {
    reset();
  };

  return <Screen onLoginSuccess={handleLoginSuccess} onLogout={handleLogout} />;
};

initialState: { user: null }, screens: { welcome: { render: props => <WelcomeScreen {...props} />, on: { login: 'dashboard' }, }, dashboard: { render: props => <DashboardScreen {...props} />, on: {}, }, }, };

const MyComponent = () => { const { goBack, Screen } = useStatefulScreens(config); return ; };


## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.

## License

[MIT](https://choosealicense.com/licenses/mit/)