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

v2.0.3

Published

Spawns React Components in a non-SPA environment

Downloads

21

Readme

React Spawner

react-spawner allows you to mix and match the old web application paradigm of using jQuery or any other enhancing JavaScript code and React. It's a great solution for people who want to use some React components in an existing website.

Howto install

Step by step on how to implement react-spawner into your project.

Extend the base class

Extend the Base Spawner like you would do with any ES6 class.

import Spawner from 'react-spawner';

export default class GrandSpawner extends Spawner
{
    
}

Extend the Constructor

Add a list of all the DOM elements that should be filled on page render with the Components that should be rendered in that div/span/...

    constructor()
    {
        super();
        
        //Set an array of objects that need to be spawned, you could include an json file, like in example/
        this.setSpawnList([{
            "name": "HelloWorld",
                "props": {},
                "target": "ngr-target-games",
                "jsx": null
        }]);
        
        //Initiate the spawner
        this.spawner(this.getSpawnList());
    }

Override the classFiller method

Add a method in your Spawner class that converts the "name" parameter to a JSX react component. The parameter component is required!

import Spawner from 'react-spawner';
import HelloWorld from './HelloWorld.react.js';

export default class GrandSpawner extends Spawner
{
    ...
    
    classFiller(component)
    {
        switch(component.name)
        {
            //Add the component types you want to expose to the spawner
            case "HelloWorld":
                component.jsx = <HelloWorld />;
                break;
        }
        
        return component;
    }
    
    ...
}

Add the ComponentSpawner

Add the component spawner to your main.js file.

import ComponentFactory from './GrandSpawner.react.js';

var application = new GrandSpawner();

and expose the public API in the same file (will be updated in the next version)

window.reactApplication = {
    /**
     * {@inheritdoc}
     * @returns {Object}
     */
    getSpawns: function()
    {
        return application.getSpawns();
    },

    /**
     * {@inheritdoc}
     * @returns {Object}
     */
    reload: function()
    {
        return application.reload();
    },

    /**
     * {@inheritdoc}
     * @returns {Object}
     */
    spawnUnregistered: function(domElement, ReactClass, props)
    {
        return application.spawnUnregistered(domElement, ReactClass, props);
    }
};

You've now installed react-spawner.

Usage

By default, react-spawner will check your list on page-render, and will inject the components that have a DOMNode on the page.

If you want to re-run the react-spawner on the fly, because you for instance have added a new DOMNode with jQuery, you can run reactApplication.reload. This will apply the diff of the component list you've supplied.

If you want to append a component to the DOM that wasn't defined in the componentlist, you can use the reactApplication.spawnUnregistered(domElement, ReactClass, props) method. If the domElement (the id of a DOMNode), is found and the ReactClass is defined in the classFiller, a new component will spawn.