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-components-compose

v0.0.3

Published

Read this in other languages: [简体中文](https://github.com/wsafight/react-components-compose/blob/main/README.zh-CN.md)

Downloads

10

Readme

react-components-compose

Read this in other languages: 简体中文

Build Status NPM Version

Use compose to combine React components, which is convenient to combine and replace React components.

Installation

npm install react-components-compose

or

yarn add react-components-compose

Usage

Simple component display

The outer middle and inner will be shown below

import reactComponentsCompose from 'react-components-compose'

const Outer = (props) => {
  // children​ points to the child nodes of the current ​react element​
  const { children } = props;
  return <>
    <div>outer</div>
    {children}
  </>
}

const Middle: React.FC<any> = (props) => {
  const { children } = props;
  return <>
    <div>middle</div>
    {children}
  </>
}

const Inner: React.FC<any> = (props) => {
  const { children } = props;
  return <>
    <div>inner</div>
  </>
}


export const SimpleShow = () => {
  const components = [Outer, Middle, Inner]
  return (
    <div>
      {reactComponentsCompose(components)}
    </div>
  );
};

Adjust props

import { reactComponentsCompose } from "react-components-compose";

const Outer = (props) => {
  const { children, data } = props;
  // show outer 123
  return (
    <>
      <div>outer {data}</div>
      {children}
    </>
  );
};

const Middle = (props) => {
  const { children } = props;
  // Pass in additional props via cloneElement
  return (
    <>
      <div>middle</div>
      {React.cloneElement(children, { data: 789 })}
    </>
  );
};

const Inner = (props) => {
  const { data } = props;
  // The obtained data is 789, showing inner 789
  return (
    <>
      <div>inner {data}</div>
    </>
  );
};

export const SimpleShowWithProps = () => {
  // Pass component props in turn
  const components = [[Outer, {data: 123}], Middle, [Inner, {data: 456}]];
  return (
    <div>
      {reactComponentsCompose(components)}
    </div>
  );
};

Add event notification

import { reactComponentsCompose } from "react-components-compose";
import mitt from "mitt";

// No other functions, only responsible for event clearing
const EventClear = (props) => {
  const { children, bus } = props;

  // Used to clear the data in the bus
  React.useEffect(() => {
    return () => {
      bus.all.clear();
    };
  }, []);

  return (
    <>
      {React.cloneElement(children, { bus })}
    </>
  );
};

class OuterClass extends React.Component<any, any> {
  constructor(props) {
    super(props);
    this.state = {
      time: 0,
    };
  }
  handleClick = () => {
    // Trigger out click with parameter 123
    this.props.bus.emit("out click", 123);
  };
  render(): React.ReactNode {
    const { data, bus, children } = this.props;
    return (
      <>
        {data}
        <div onClick={this.handleClick}>outer</div>
        {React.cloneElement(children as React.ReactElement, { bus })}
      </>
    );
  }
}

const Inner = (props) => {
  const { data, bus } = props;
  const [time, setTime] = React.useState(0);
  React.useEffect(() => {
    // * means to listen to all events, type is the event name, params is the event value
    bus.on("*", (type, params) => {
      console.log(type, params);
      alert(params);
    });
  }, []);

  const handleClick = () => {
    setTime(time + 1);
  };

  return [
    <div>{time} {data}</div>,
    <div onClick={handleClick}>inner</div>,
    <div>inner2</div>,
    <div>inner3</div>,
  ];
};

export const ArrayShowWithEvent1 = () => {
  const components = [[EventClear, {bus: mitt()}], OuterClass, [Inner, {data: times}]];
  return (
    <div>
      {reactComponentsCompose(components)}
    </div>
  );
};

// Consider not clearing mitt processing, for components, there are "global" variables
export const ArrayShowWithEvent3 = () => {
  const bus = mitt();

  // Clear mitt event binding can be written here

  const components = [
    [OuterClass, { bus }],
    [Inner, { data: "times", bus }],
  ];
  return (
    <div>
      {reactComponentsCompose(components)}
    </div>
  );
};