1.0.29 • Published 6 years ago

woo-sync v1.0.29

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Woo-Sync

Build Status Coverage Status npm version

A helper for React/Mobx project.

It provides more handy way to access values in Mobx store from your <Component />.

Installation

$ yarn add woo-sync

Or

$ npm install woo-sync

Usage

The woo-sync package contains two separete packages SyncProvider and syncData

SyncProvider

Initially the React tree should be wrapped with SyncProvider.

SyncProvider is a wrapper component which passes store through context to all children components.

import React from 'react';
import ReactDOM from 'react-dom';
import { SyncProvider } from 'woo-sync';

import store from '../mobx-store';
import MyComponent from '../MyComponent';

const App = () => (
   <SyncProvider store={store}>
    <MyComponent />
   </SyncProvider>
);


ReactDOM.render(<App />, node);

syncData

To pass needed store values to <MyComponent /> we need to use syncData HOC component to wrap <MyComponent />.

import { syncData } from 'woo-sync';

const MyComponent = (props) => [
  <div>{props.name}</div>,
  <button onClick={props.changeName} type="button">Change name</button>
];

export default syncData({
  name: ['user', 'name'],
  changeName: ['user', 'changeName']
})(MyComponent);

Two ways of passing data to syncData is using plain object or callback function

Plain Object
syncData({
  name: ['user', 'name'],
  changeName: ['user', 'changeName']
})(MyComponent)
Callback function
syncData((props, state) => { // Arguments are current props and mobx store from context
  return {
    name: ['user', 'name'],
    changeName: ['user', 'changeName']
  };
})(MyComponent)

Mobx store looks like this :point_down:

import { action, computed, observable } from 'mobx';

class User {
  @observable username = null;
  
  @action changeName() {
    this.username = 'John';
  }
  
  @computed get name() {
    return this.username;
  }
}

export default { // will be passed as a prop to SyncProvider
  user: new User()
};

:bear: :tiger: :octopus: :snail: :squirrel:

1.0.29

6 years ago

1.0.27

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.19

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago