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-extensible

v2.0.0

Published

A library which helps you create extensible react applications

Downloads

34

Readme

In the Name of Allah

React-Extensible

A library which helps you create extensible React applications.

Table of contents

Installation

Via npm:

npm install react-extensible --save

Via yarn:

yarn add react-extensible

Basic Usage

react-extensible exports:

  1. Extension: it is a React component that represents an extension or plugin. It receives a prop called name; You can have several Extensions with the same name. another prop is render, its value is a React component/element.
  2. Actions: is a static class. You can register or unrgister an extension using Actions.register(extnInfo) and Actions.unregister(extnName), extnInfo is an object and at least must have a name field.
import React from 'react';
import {Extension, Actions} from 'react-extensible';

const App = props => (
  <div className="App">
    <Extension name="alpha" render={<div>Hello</div>}/>
    <Extension name="beta" render={() => <div>Hello</div>}/>
    <Extension name="beta" render={<div>Hello</div>}/>
  </div>
);

Actions.register({
  name: "alpha"
});
Actions.register({
  name: "beta"
});
setTimeout(() => Actions.disable("beta"), 3000)

export default App;

Result: You will observe three "Hello" messages on the screen. after 3 seconds, the two last ones will get disappeared because of Action.disable("beta").

react-extensible has much more capabilities; See API Reference.

API Reference

Actions

A static class; It has five methods:

  1. register(extnInfo: Object): registers an extension so you can use the information of the registered extension to display the extension in your app, you can even display an extension at several places in your app.

extnInfo:

{
  name: String, // required, the name of the extension
  disable: Boolean, // default false, if true, the extension will get unmounted
  render: Component/Element, // the extension will render this React component/element if none of props.render and props.children of the extension component whose props.name is equal to the name property of this object.
  props: Object, // default {}, these props will be passed to the component that the extension component renders.
}
  1. unregister(extnName): unregisters an extension in other hand remove an extnInfo object.
  2. disable(extnName): disables an extension so the extension will be unmounted.
  3. enable(extnName): enables an extension so the extension will be mounted again.
  4. get(extnName): returns an extnInfo object according to its name; if there is no matching extnInfo object, returns undefined. If you pass nothing (undefined), it will return the store (the extnInfo objects).
  5. update(fn: (prevStore: any[]) => store): updates the store to the value that its callback returns.

Extension

A React component; It represents an extension. props:

  1. name: is required, it links an extension to a registered extnInfo object.
  2. fallback: a component which will get rendered when the ErrorBoundary catches an error in its child component tree (the component that the extension renders); This fallback component receives a prop named error which consists the occurred error.
  3. props: an object, these props will be passed to the component that the extension renders.
  4. render: a React component/element, the extension will render this component/element.
  5. children: a React component/element, The extension will always render this component/element, even if the extension is disabled.

Note: the props of the Extension component will override the relevant extnInfo object.

Map

A React component; It will call a function for each registered extnInfo object once it is rendered and anytime a change happens in the store (the extnInfo objects).

The function is received through children. In fact, it will be passed to a map function so it receives each extnInfo object as the first argument. It can return nothing or an Extension:

import React from 'react';
import {Extension, Map, Actions} from 'react-extensible';

const App = props => (
  <div className="App">
    <Map>
      {extnInfo => <Extension name={extnInfo.name} render={extnInfo.name === "alpha" && (<div>Hello, I'm alpha</div>)}/>}
    </Map>
    <Map>
      {extnInfo => {
        if(extnInfo.name === "alpha")
          return <Extension name="alpha"/>
        }}
    </Map>
  </div>
);

Actions.register({
  name: "alpha",
  render: <div>Hi, I am Alpha</div>
});

Actions.register({
  name: "beta",
  render: () => <div>I'll go soon...:(</div>
});

setTimeout(() => Actions.disable("beta"), 3000);

export default App;

Examples

Several ReactDOM.renders

import React from 'react';
import ReactDOM from 'react-dom';
import {Extension, Map, Actions} from 'react-extensible';

Actions.register({
  name: "alpha",
  render: <div>Hi, I am Alpha</div>
});

ReactDOM.render(<Map>
  {extnInfo => <Extension name={extnInfo.name} render={extnInfo.name === "alpha" && (<div>Hello, I'm alpha</div>)}/>}
</Map>,
document.getElementById('a'));

ReactDOM.render(<Map>
  {extnInfo => {
    if(extnInfo.name === "alpha")
      return <Extension name="alpha"/>
    }}
</Map>, document.getElementById('b'));

Actions.register({
  name: "beta",
  render: () => <div>I'll go soon...:(</div>
});

setTimeout(() => Actions.disable("beta"), 3000);

Is something missing?! feel free to open an issue!

License

Apache 2.0