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

mobx-async-connect

v2.0.4

Published

How do you usually request data and store it to mobx store? You create actions that do async jobs to load data, create store to save this data to mobx state, then connect data to your component or container.

Downloads

5

Readme

mobx-async-connect

How do you usually request data and store it to mobx store? You create actions that do async jobs to load data, create store to save this data to mobx state, then connect data to your component or container.

Usually it's very similar routine tasks.

Also, usually we want data to be preloaded. Especially if you're building universal app, or you just want pages to be solid, don't jump when data was loaded.

This package consist of 2 parts: one part allows you to delay containers rendering until some async actions are happening. Another stores your data to mobx store and connect your loaded data to your container.

Notice

This is a fork, merge and refactor of redux-async-connect and redux-connect for Mobx.

Installation & Usage

Using npm:

$ npm install mobx-async-connect -S

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'mobx-react';
import { browserHistory, Router, Route } from 'react-router';
import { MobxAsyncConnect, asyncConnect, store as mobxAsyncConnect } from 'mobx-async-connect';

// 1. Connect your data, similar to mobx-react @inject
@asyncConnect([{
  key: 'lunch',
  promise: ({ store, location, params }) => Promise.resolve({ id: 1, name: 'Borsch' })
}])
class App extends React.Component {
  render() {
    // 2. access data as props
    const lunch = this.props.lunch
    return (
      <div>{lunch.name}</div>
    );
  }
}

// 3. Connect mobx async store
const initialState = window.__data || null;
const store = {
  mobxAsyncConnect: new mobxAsyncConnect(initialState && initialState.mobxAsyncConnect || undefined)
};

// 4. Render `Router` with MobxAsyncConnect middleware
render((
  <Provider {...store}>
    <Router render={props => <MobxAsyncConnect {...props} filter={item => !item.deferred} />} history={browserHistory}>
      <Route path="/" component={App}/>
    </Router>
  </Provider>
), el);

Server

import { renderToString } from 'react-dom/server';
import { match } from 'react-router';
import { Provider } from 'mobx-react';
import { MobxAsyncConnect, loadOnServer, store as mobxAsyncConnect } from 'mobx-async-connect';
import createHistory from 'react-router/lib/createMemoryHistory';
import serialize from 'serialize-javascript';

app.get('*', (req, res) => {
  const store = {
    mobxAsyncConnect: new mobxAsyncConnect()
  };

  match({ history, routes, location: req.url }, (err, redirect, renderProps) => {

    // 1. load data
    loadOnServer({ ...renderProps, store }).then(() => {

      // 2. use `MobxAsyncConnect` instead of `RouterContext` and pass it `renderProps`
      const appHTML = renderToString(
        <Provider {...store}>
          <MobxAsyncConnect {...renderProps} />
        </Provider>
      );

      // 3. render the Mobx initial data into the server markup
      const html = createPage(appHTML, store);
      res.send(html);
    });
  });
});

function createPage(html, store) {
  return `
    <!doctype html>
    <html>
      <body>
        <div id="app">${html}</div>

        <!-- its a Mobx initial data -->
        <script dangerouslySetInnerHTML={{__html: `window.__data=${serialize(store)};`}} charSet="UTF-8"/>
      </body>
    </html>
  `;
}

API

By default render props of the MobxAsyncConnect component is:

const render = props => <RouterContext {...props} />;

All additionnals 'parameters' (eg: helpers, data fetcher) added to props of MobxAsyncConnect component(s) are accessible in promise options, beside store, location, params and others.

Note: For universal application it is highly recommended to have the same client-side props than server side.

Contributors

Collaboration

You're welcome to PR, and we appreciate any questions or issues, please open an issue!