0.0.9 • Published 4 years ago

run-state v0.0.9

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Build Status

run-state

Edit kind-banzai-n5lrb

import React, { useEffect } from 'react';
import { useData, RunStateProvider } from 'run-state';

const Demo = () => {
  const { getState, updateState, runAction } = useData();

  const update = () => updateState('GET_POST', post => ({ ...post, title: 'oh sh*t' }));

  useEffect(() => {
    runAction("GET_POST", 1);
  }, []);

  const { loading, data } = getState("GET_POST");

  return (
    <div>
      {loading ? "⌛" : "✅"}
      <pre>{JSON.stringify(data, null, 4)}</pre>
      <button onClick={update}>update</button>
    </div>
  );
};

const AnotherDemo = () => {
  const { getState } = useData();

  const { loading, data } = getState("GET_POST");

  return (
    <div style={{ padding: "1rem", border: "solid 1rem #ccc" }}>
      {loading ? "⌛" : "✅"}
      <pre>{JSON.stringify(data, null, 4)}</pre>
      {null}
    </div>
  );
};

const store = {
  GET_POST: {
    action: id =>
      new Promise(resolve => {
        setTimeout(() => {
          fetch("https://jsonplaceholder.typicode.com/posts/" + id)
            .then(response => {
              return response.json();
            })
            .then(data => resolve(data));
        }, 1000);
      })
  }
};

export default function App() {
  return (
    <RunStateProvider store={store}>
      <Demo />
      <AnotherDemo />
    </RunStateProvider>
  );
}
0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago