falcor-react-toolkit
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

falcor-react-toolkit

npm version MIT License

falcor for react hooks

  • react >= 16.8.0

usage

FalcorProvider

import React from 'react';
import Falcor from 'falcor';
import { FalcorProvider } from 'falcor-react-toolkit';

const model = new Falcor.Model({
  cache: {
    foo: 'bar',
    todos: [
      { name: 'get milk from corner store' },
      { name: 'withdraw money from ATM' },
    ]
  }
});

return (
  <FalcorProvider model={model}>
    <App />
  </FalcorProvider>
)

useFalcor hooks

import React from 'react';
import { useFalcor } from 'falcor-react-toolkit';

const App = (props) => {
  const path = ['foo'];
  const { loading, error, data } = useFalcor(path);

  return (
    <div>
      {loading && <span>Now Loading...</span>}
      {!loading && (
        <div>
          {data.foo}
        </div>
      )}
    </div>
  )
};

useFalcor with normalizer

import React from 'react';
import { useFalcor } from 'falcor-react-toolkit';

const App = (props) => {
  const path = ['todos', { from: 0, to: 10 }, ['name']];
  const normalizer = (json) => {
    return Object.entries(json.todos)
    .filter(([key, _]) => key !== '$__path')
    .map(([_, val]) => val);
  };
  const { loading, error, data } = useFalcor(path, { normalizer });

  return (
      <div>
      {loading && <span>Now Loading...</span>}
      {!loading && (
        <div>
          {data.map(value => <div>{value.name}</div>)}
        </div>
       )}
      </div>
      )
};

useFalcor with batchModel

import React from 'react';
import { useFalcor } from 'falcor-react-toolkit';

const App = (props) => {
  const path = ['foo'];
  const batch = {key: 'default-batch-model', delay: 100};
  const { loading, error, data } = useFalcor(path, { batch });

  return (
      <div>
      {loading && <span>Now Loading...</span>}
      {!loading && (
          <div>
          {data.foo}
          </div>
          )}
      </div>
      )
};

useFalcorModel

import React from 'react';
import { useFalcorModel } from 'falcor-react-toolkit';

const App = (props) => {
  const model = useFalcorModel();
  ...
}

Readme

Keywords

Package Sidebar

Install

npm i falcor-react-toolkit

Weekly Downloads

1

Version

0.4.0

License

MIT

Unpacked Size

9.01 kB

Total Files

8

Last publish

Collaborators

  • nmurayama