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

v2.1.1

Published

HOC that allows ioc for presenter (Uis) to wrap themselves in containers passing themselves to the container as props.container.

Downloads

9

Readme

react-withcontainer

HOC that allows ioc for Uis to wrap themselves in containers passing themselves to the container as props.container.

Installation

Install with npm:

npm install react-withcontainer

Or with yarn:

yarn add react-withcontainer

Usage

Container

Your container should contain all the normal logic for containers under the container-presenter pattern, and should expect to receive an additional field component represeting the Prenter Component in its props as props.component.

// ExampleContainer.tsx
import * as React from "react";
import { ContainerComponentProps } from "react-withcontainer";

/**
 * This is the props for the container.
 */
export interface ExampleContainerProps extends ContainerComponentProps {
    // Additional props for the container.
}

interface Model {
    id: string,
    name: string
}

/**
 * These are the props the container will pass on to its Presenter/Ui.
 */
export interface ExampleContainerUiProps {
    model: Model | undefined,

    changeModel: (changes: Partial<Model>) => void,
    load: () => Promise<boolean>
    save: () => Promise<boolean>
}


export const ExampleContainer = (props: ExampleContainerProps) => {
    const { component, id, ...rest } = props;
	
    const [model, setModel] = React.useState<Model | undefined>(undefined);

	// Change the fields in the model in a controlled way using setModel.
    const changeModel = React.useCallback((changes: Partial<UserProfile>) => {
        setModel(prevState => ({
            ...(prevState as UserProfile),
            ...changes
        }));
    }, [setModel]);

    // Example method to load a model from storage.
    const load = React.useCallback(async (): Promise<boolean> => {
        let result = await loadModel(id); // Replace loadModel() with your custom loading logic.
        setModel(result);
        return true;
    }, [setModel, id]);

    // Example method to save a model to storage.
    const save = React.useCallback(async (): Promise<boolean> => {
        if (!model) {
            return false;
        }

        let ok = await saveModel(model.id, model, isCreate); // Replace saveModel() with your actual code to save the model to stroage.
        return ok;
    }, [model, isCreate]);

    // Load on mount if we haven't got a model.
    React.useEffect(() => {
        if (!model && !isLoading && !loadingErrors) {
            load();
        }
    }, [model, isLoading, loadingErrors, load]);

    let Component = component;

    return (
        <Component {...rest} model={model} laod={load} save={save} />
    );
};

Presenter

Your presenter accepts all the props passed by the component and should focus only on providing a Ui. It can then use the withContainer() HOC to wrap itself in an appropriate compatable container.

// ExampleUi.tsx
import * as React from "react";
import { withContainer } from "react-withcontainer";
import { Form, FormGroup, Label, Button, Container, Input } from 'reactstrap';
import { ExampleContainer, ExampleContainerUiProps } from 'ExampleContainer'; // This contains the example component from above.

export const ExampleUi = (props: ExampleContainerUiProps) => {
    const onSubmit = React.useCallback(async (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        await props.save();
    }, [props.save]);

	const onChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
        var target = event.currentTarget;
        props.changeModel({
            [target.name]: (target.type == 'checkbox' ? target.checked : target.value)
        });
    }, [props.changeModel]);

    return (
        <Container>
            <Form onSubmit={onSubmit}>
                <FormGroup>
                    <Label htmlFor="name">Name</Label>
                    <Input type="text" name="name" placeholder="Name" value={props.model.name} onChange={onChange}  />
                </FormGroup>

                <div>
                    <Button type="submit" color="primary">
                        Save
                    </Button>
                </div>
            </Form>
        </Container>
    );
};

export const Example = withContainer(ExampleContainer)(ExampleUi);

Typescript

This project is written in typescript and comes with its own bindings.

License

Licensed under the MIT license.