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-component-switch

v1.0.0

Published

A React component for creating step-based applications from a list of other components.

Downloads

3

Readme

react-component-switch

react-component-switch is a react component for rendering child components from a list of React.Component class constructors. react-component-switch provides no return Object itself and will render null if a child component is not provided. It sits as a logic layer between a parent element and a child component, allowing a traversable list of components to be rendered in a single spot. This is useful for any part of an application that needs to support a step system such as Wizards, Account Creation forms, or Two Factor Authentication.

- Version: 1.0.0

Table of Contents

Installing

npm install react-component-switch --save

Getting Started

const ComponentSwitch = require('react-component-switch');

or

import ComponentSwitch from 'react-component-switch';

Usage and Examples

Creating a List of Child Components

Components are passed to ComponentSwitch as an array assigned to the prop 'components'. Imagine we have 2 components in separate files that we want to pass, Component 'Hello' and Component 'Goodbye':

import React from 'react';
import ReactDOM from 'react-dom';
import ComponentSwitch from 'react-component-switch';

import Hello from 'hello';
import Goodbye from 'goodbye';


const componentList = [Hello, Goodbye];     // Store the class constructors in an array.

// ComponentSwitch is rendered with componentList array as prop 'components';
ReactDOM.render(<ComponentSwitch components={components} />, document.getElementById('app'));

Traversing The List

When a child component is rendered from the list, it is passed a reference to ComponentSwitch as a prop. This gives every component in our list access to ComponentSwitch's methods. ComponentSwitch has 3 methods: 'next', 'back', and 'setStep'. next and back are used for single step jumps in their respective directions, where setStep can be used to jump to any step in the list. setStep also exposes access to 'Side Steps', which are components that ComponentSwitch can render that are not part of our main component list. This feature allows conditional and temporary step rendering. Useful for things like Loading screens. Child components can access all of these methods like so:


/* From within the child component source */

// Trigger next component in list
this.props.ComponentSwitch.next();

// Trigger previous component in list
this.props.ComponentSwitch.back();

// Trigger any step. Using a number as the argument for main component list steps and a string to trigger side component steps.
this.props.ComponentSwitch.setStep(3);

Passing Side components

Side components are given to ComponentSwitch in an Object as prop 'sides'. Each side component should have a name:value pair conforming to the format componentName:constructor. This way the component can be rendered by simply passing the components name as a string to the setStep method.


import React from 'react';
import ReactDOM from 'react-dom';
import ComponentSwitch from 'react-component-switch';

import Hello from 'hello';
import Goodbye from 'goodbye';
import HowAreYou from 'howAreYou';

const componentList = [Hello, Goodbye];     // Store the class constructors in an array.
const sideComponents = { HowAreYou };        // ES6 equivalent of { "HowAreYou": HowAreYou };

// ComponentSwitch is rendered with componentList array as prop 'components';
ReactDOM.render(<ComponentSwitch components={components} sides={sideComponents} />, document.getElementById('app'));

When a side component is rendered, triggering the 'next' or 'back' method will render their respective components from the main list. If an event in step 2 were to trigger the rendering of a side component, then if the 'next' method is triggered from within the Side component, step 3 from the main list will be rendered. This supports the temporary nature of Side components.