How to use the react-sweet-state.createStore function in react-sweet-state

To help you get started, we’ve selected a few react-sweet-state examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / theme.js View on Github external
const initialState: State = {
  color: '',
};

const actions = {
  change: (value?: string) => (
    { setState }: StoreActionApi,
    { defaultColor }: ContainerProps
  ) => {
    setState({
      color: value || defaultColor,
    });
  },
};

const Store = createStore({
  name: 'theme',
  initialState,
  actions,
});

export const ThemeContainer = createContainer(
  Store,
  {
    onInit: () => ({ getState, dispatch }) => {
      // this gets currently called also when component remounts
      // so it is important to check state status and apply default only on first mount
      const { color } = getState();
      if (!color) {
        dispatch(actions.change());
      }
    },
github atlassian / react-sweet-state / examples / performance-test / components / todos.js View on Github external
order: getState().order.concat(newTodo.id),
    });
  },

  toggle: (todoId: number) => ({ setState, getState }: StoreApi) => {
    const todo = getState().byId[todoId];
    setState({
      byId: {
        ...getState().byId,
        [todo.id]: { ...todo, isDone: !todo.isDone },
      },
    });
  },
};

const Store = createStore({
  name: 'todos',
  initialState,
  actions,
});

/** Container */

type ContainerProps = {| n: number |};
export const TodosContainer = createContainer(
  Store,
  {
    onInit: () => ({ getState, dispatch }, { n }) => {
      if (getState().order.length) return;
      Array.from({ length: 10 * n }).map((__, i) => {
        const title = `Todo ${n}-${i + 1}`;
        dispatch(actions.add(title));
github atlassian / react-sweet-state / examples / basic-ts / components.tsx View on Github external
count: number;
};

const initialState: State = {
  count: 0,
};

const actions = {
  increment: () => ({ setState, getState }: StoreActionApi) => {
    setState({
      count: getState().count + 1,
    });
  },
};

const Store = createStore({
  initialState,
  actions,
});

export const CounterSubscriber = createSubscriber(Store);

export const useCounter = createHook(Store);
github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / messages.js View on Github external
type Actions = typeof actions;

const initialState: State = {
  data: [],
  loading: false,
};

const actions = {
  add: (message: string) => ({ setState, getState }: StoreActionApi) => {
    setState({
      data: [...getState().data, message],
    });
  },
};

const Store = createStore({
  name: 'messages',
  initialState,
  actions,
});

export const MessagesSubscriber = createSubscriber(Store);

export const useMessages = createHook(Store);
github atlassian / react-sweet-state / examples / basic-flow / components.js View on Github external
type Actions = typeof actions;

const initialState: State = {
  count: 0,
};

const actions = {
  increment: () => ({ setState, getState }: StoreActionApi) => {
    setState({
      count: getState().count + 1,
    });
  },
};

const Store = createStore({
  initialState,
  actions,
});

export const CounterSubscriber = createSubscriber(Store);

export const useCounter = createHook(Store);
github dai-shi / will-this-react-global-state-work-in-concurrent-mode / src / react-sweet-state / index.js View on Github external
import React, { useTransition } from 'react';
import { createStore, createHook } from 'react-sweet-state';

import {
  syncBlock,
  useRegisterIncrementDispatcher,
  initialState,
  reducer,
  ids,
  useCheckTearing,
  shallowEqual,
} from '../common';

const Store = createStore({
  initialState,
  actions: {
    dispatch: (action) => ({ setState, getState }) => {
      setState(reducer(getState(), action));
    },
  },
});

const useCounter = createHook(Store);

const Counter = React.memo(() => {
  const [state] = useCounter();
  const { count } = state;
  syncBlock();
  return <div>{count}</div>;
}, shallowEqual);
github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / form.js View on Github external
},

  send: (message: string) =&gt; async ({ setState }: StoreApi) =&gt; {
    setState({
      isSending: true,
    });
    await new Promise(r =&gt; setTimeout(r, 1000));
    setState({
      isSending: false,
      message: '',
    });
    return message;
  },
};

const Store = createStore({
  name: 'form',
  initialState,
  actions,
});

export const FormContainer = createContainer(
  Store,
  {
    onUpdate: () =&gt; ({ setState }, { remoteUsers }) =&gt; {
      setState({ toUsers: remoteUsers });
    },
  }
);

export const FormSubscriber = createSubscriber(Store);
github atlassian / react-sweet-state / examples / advanced-flow / components / todo / index.js View on Github external
createSubscriber,
  createHook,
} from 'react-sweet-state';
import type { State } from './types';

import * as actions from './actions';

type Actions = typeof actions;
type ContainerProps = {| selectedUser: string | null |};

const initialState: State = {
  data: null,
  loading: false,
};

const Store = createStore({
  initialState,
  actions,
});

export const TodoContainer = createContainer(
  Store,
  {
    onUpdate: () =&gt; ({ dispatch }, { selectedUser }) =&gt; {
      if (selectedUser) dispatch(actions.load(selectedUser));
    },
  }
);

export const TodoSubscriber = createSubscriber(Store);

export const useTodo = createHook(Store);
github atlassian / react-sweet-state / examples / advanced-flow / components / user / index.js View on Github external
import type { State } from './types';

import * as actions from './actions';
import * as selectors from './selectors';

type Actions = typeof actions;
type ContainerProps = {||};
type UserSelectedState = $Call;

const initialState: State = {
  selected: null,
  data: null,
  loading: false,
};

const Store = createStore({
  initialState,
  actions,
});

export const UserContainer = createContainer(
  Store,
  {
    onInit: actions.load,
  }
);

export const UserSubscriber = createSubscriber(Store);

export const UserSelectedSubscriber = createSubscriber&lt;
  State,
  Actions,

react-sweet-state

Global + local state combining the best of Redux and Context API

MIT
Latest version published 3 months ago

Package Health Score

82 / 100
Full package analysis