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

urc

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

urc

Universal react connect decorator builder

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-78.95%
Maintainers
1
Weekly downloads
 
Created
Source

Universal react connect

Helpers for creating tools like react-redux, mobx-react with minimal boilerplate.

Mobx simple example

import {createConnect} from 'urc'
import createMobxAtom from 'urc/createMobxAtom'
import {observable, Reaction} from 'mobx'
import React from 'react'

const connect = createConnect({
    ReactAtom: createMobxAtom(Reaction),
    BaseComponent: React.Component
})

class Store {
    @observable name = 'test'
}

const store = new Store()

const MyPureComponent = connect(() =>
    <div>
        {store.name}
        <br/>
        <input value={store.name} onInput={({target}) => store.name = target.value}/>
    </div>
)
// or

@connect class MyComponent extends React.Component {
    render() {
        // ...
    }
}

Error handling example

Connected component calls renderError if exception throwed in render.

// ...
function renderError({error, children}) {
    return <div>
        <pre>{error.stack.toString()}</pre>
    </div>
}

const connect = createConnect({
    ReactAtom: createMobxAtom(Reaction),
    BaseComponent: React.Component,
    renderError
})

const MyPureComponent = connect(() => {
    throw new Error('test')
})

Custom component mixin

import {ObserverComponent, createConnect} from 'urc'

class MyObserverComponent<Props, State, Context, Element> extends ObserverComponent<Props, State, Context, Element> {
    static instance: number

    componentWillMount() {
        super.componentWillMount()
        const cns: Function = this.constructor
        if (cns.instance === undefined) cns.instance = 0

        cns.instance++
    }

    componentWillUnmount() {
        this.constructor.instance--
        super.componentWillUnmount()
    }

    __value(isPropsChanged: boolean) {
        const value = super.__value(isPropsChanged)
        return value
    }

    _getContext(key: Function, propsChanged: boolean): Context {
        return this.context
    }
}

const connect = createConnect({
    ReactAtom: createMobxAtom(Reaction),
    BaseComponent: React.Component,
    MixinComponent: MyObserverComponent
})

const MyPureComponent = connect(() => {
    // ...
})

lom_atom example

import {createConnect} from 'urc'
import {mem, ReactAtom} from 'lom_atom'
import React from 'react'

const connect = createConnect({
    ReactAtom,
    BaseComponent: React.Component
})

class Store {
    @mem name = 'test'
}

const store = new Store()

const MyPureComponent = connect(() =>
    <div>
        {store.name}
        <br/>
        <input value={store.name} onInput={({target}) => store.name = target.value}/>
    </div>
)

Keywords

FAQs

Package last updated on 15 Mar 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