Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aquigorka/compose

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aquigorka/compose

React component composition helper

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Compose

React component composition helper. Benefits:

  • Split view from store and/or side effects, basically from any non-view logic.
  • Split stores from side effects.
  • No need for extra libraries, keep everything in Components.

// view should be able to handle the possible states (eg. loading)
const View = ({ value, valueLoading }) => valueLoading
  ? <div>Loading...</div>
  : <div>{value}</div>

// store: get, put, post, delete, etc
class Store extends Component {
  state = { value: null, loading: false }

  render() {
    const { children, ...rest } = this.props
    const { value, loading } = this.state

    <Fragment>
      {Children.map(children, child =>
        cloneElement(child, {
          ...rest,
          valueGet: this.get,
          valuePost: this.post,
          valueLoading: loading,
          value
        }),
      )}
    </Fragment>
  }

  get = () => {
    // fetch value
    this.setState({ loading: true })
    setTimeout(() => this.setState({ value: 1, loading: false }), 1000)
  }

  post = () => { /*...*/ }
}

// side effects, like fetching value on mount
class Saga extends Component {
  componentDidMount() {
    this.props.valueGet()
  }

  render() {
    const { children, ...props } = this.props
    const { valuePost, ...rest } = props

    <Fragment>
      {Children.map(children, child =>
        cloneElement(child, { ...rest, valuePost: this.post }),
      )}
    </Fragment>
  }

  post = data => {
    // handle side effects, then dispatch the action further up
    // possible side effect eg. some other request
    this.props.post(data)
  }
}

const SagaStore = Compose(Store, Saga)

export { SagaStore as Store, View }
export default Compose(SagaStore, View)

Keywords

FAQs

Package last updated on 23 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc