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

with-mobx-store

v1.1.6

Published

HOC library for MobX Provider wrapping.

Downloads

6

Readme

with-mobx-store

npm package

with-mobx-store is higher order components library for performing automated MobX Provider wrapping.

Installation

npm i mobx mobx-react with-mobx-store
yarn add mobx mobx-react with-mobx-store

Concept

When you want to work with MobX store, you have to create at least two components to work with it as the following example below.

import React from 'react'
import { Provider } from 'mobx'
import DataHandler from './DataList'
import DataStore from './store'
const dataStore = new DataStore()
const DataListContainer = () => (
    <Provider dataStore={dataStore}>
        <DataHandler />
    </Provider>
)
export default DataListContainer

I want to inject DataStore to DataHandler. I have to create a component for wrapping DataHandler with Provider. After that I have to create another component DataHandler to inject dataStore to it like the following code.

import React, { Fragment } from 'react'
import { inject, observer } from 'mobx-react'
import DataItem from './DataItem'
import { compose } from 'recompose'
const DataHandler = ({ dataStore }) => (
    <Fragment>
        { dataStore.data.map(datum => (
            <DataItem
                key={datum.id}
                {...datum}
            />
        ) }
    </Fragment>
)
export default compose(
    inject('dataStore'),
    observer,
)(DataHandler)

Is it better if we don't have to create two new component just use the higher order component to wrap our target componnent?

This is why we create with-mobx-store

Usage

withStore(stores, options)

withStore is a higher order component (HOC) that allow you to initialize component without wraping any provider.

You just simply wrap your component with withStore, then it's worked!

Parameters

  • stores - Object for mapping name of store with store object.
  • options - Options for modifying store lifecycle

Example

import React from 'react'
import { inject, observer } from 'mobx-react'
import { compose } from 'recompose'
import { withStore } from 'with-mobx-store'
import DataItem from './DataItem'
import DataStore from './store'
const DataHandler = ({ dataStore }) => (
    <Fragment>
        { dataStore.data.map(datum => (
            <DataItem
                key={datum.id}
                {...datum}
            />
        ) }
    </Fragment>
)
export default compose(
    withStore({
        dataStore: new DataStore(),
    }),
    inject('dataStore'),
    observer,
)(DataHandler)

withComponentStore(stores, options)

withComponentStore is higher order component (HOC) that allow you to initialize component without wraping any provider.

Moreover, it works specifically with stores per component.

Parameters

  • stores - Object for mapping name of store with store class.
  • options - Options for modifying store lifecycle.

Example

import React from 'react'
import { inject, observer } from 'mobx-react'
import { compose } from 'recompose'
import { withComponentStore } from 'with-mobx-store'
import DataItem from './DataItem'
import DataStore from './stores/DataStore'
const DataHandler = ({ data }) => (
    <Fragment>
        { dataStore.data.map(datum => (
            <DataItem
                key={datum.id}
                {...datum}
            />
        ) }
    </Fragment>
)
export default compose(
    withComponentStore({
        data: DataStore,
    }),
    inject('dataStore'),
    observer,
)(DataHandler)

Options

We provide some options that you can modify your stores after it is initialized.

onInitialized

onInitialized is a function which is executed when the initialization of stores is complete.

Example

import React from 'react'
import { inject, observer } from 'mobx-react'
import { compose } from 'recompose'
import { withComponentStore } from 'with-mobx-store'
import DataItem from './DataItem'
import DataStore from './stores/DataStore'
const DataHandler = ({ data }) => (
    <Fragment>
        { dataStore.data.map(datum => (
            <DataItem
                key={datum.id}
                {...datum}
            />
        ) }
    </Fragment>
)
export default compose(
    withComponentStore({
        data: DataStore,
    }, {
        onInitialized: (stores) => {
            stores.data.fetchData('https://api.example.com/sample')
        },
    }),
    inject('dataStore'),
    observer,
)(DataHandler)

onMounted

onMounted is executed when the component is mounted.

onUnMounted

onUnMounted is executed when the component is unmounted.

Change Log

Change Log

License

(C) 2017 Wongnai Media Co, Ltd.

with-mobx-store is licensed under MIT License