How to use the react-sweet-state.createSubscriber 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.

github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / theme.js View on Github external
export const ThemeContainer = createContainer<State, Actions, ContainerProps>(
  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());
      }
    },
  }
);

export const ThemeSubscriber = createSubscriber<State, Actions>(Store);

export const useTheme = createHook<State, Actions>(Store);
github atlassian / react-sweet-state / examples / performance-test / components / todos.js View on Github external
Array.from({ length: 10 * n }).map((__, i) => {
        const title = `Todo ${n}-${i + 1}`;
        dispatch(actions.add(title));
      });
    },
  }
);

/** Subscribers / Hooks */

const getAllTodosSelector = state => ({
  data: state.order.map(v => state.byId[v]),
  loading: state.loading,
});

export const TodosSubscriber = createSubscriber<State, Actions, *, {||}>(
  Store,
  {
    selector: getAllTodosSelector,
  }
);

export const useTodos = createHook<State, Actions, *, empty>(Store, {
  selector: getAllTodosSelector,
});

const getTodosCountSelector = state => ({ count: state.order.length });

export const TodosCountSubscriber = createSubscriber<State, Actions, *, {||}>(
  Store,
  {
    selector: getTodosCountSelector,
github atlassian / react-sweet-state / examples / performance-test / components / todos.js View on Github external
});

export const TodosSubscriber = createSubscriber<State, Actions, *, {||}>(
  Store,
  {
    selector: getAllTodosSelector,
  }
);

export const useTodos = createHook<State, Actions, *, empty>(Store, {
  selector: getAllTodosSelector,
});

const getTodosCountSelector = state => ({ count: state.order.length });

export const TodosCountSubscriber = createSubscriber<State, Actions, *, {||}>(
  Store,
  {
    selector: getTodosCountSelector,
  }
);

export const useTodosCount = createHook<State, Actions, *, empty>(Store, {
  selector: getTodosCountSelector,
});

type TodosFilteredProps = {| isDone: boolean |};

const getFilteredTodosSelector = (state: State, props: TodosFilteredProps) => ({
  data: state.order
    .map(v => state.byId[v])
    .filter(t => t.isDone === props.isDone),
github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / form.js View on Github external
const Store = createStore<State, Actions>({
  name: 'form',
  initialState,
  actions,
});

export const FormContainer = createContainer<State, Actions, ContainerProps>(
  Store,
  {
    onUpdate: () => ({ setState }, { remoteUsers }) => {
      setState({ toUsers: remoteUsers });
    },
  }
);

export const FormSubscriber = createSubscriber<State, Actions>(Store);

export const useForm = createHook<State, Actions>(Store);

export const FormActions = createSubscriber<State, Actions, void, void>(Store, {
  selector: null,
});

export const useFormActions = createHook<State, Actions, void, void>(Store, {
  selector: null,
});
github atlassian / react-sweet-state / examples / advanced-flow / components / user / index.js View on Github external
const Store = createStore<State, Actions>({
  initialState,
  actions,
});

export const UserContainer = createContainer<State, Actions, ContainerProps>(
  Store,
  {
    onInit: actions.load,
  }
);

export const UserSubscriber = createSubscriber<State, Actions>(Store);

export const UserSelectedSubscriber = createSubscriber<
  State,
  Actions,
  UserSelectedState,
  void
>(Store, {
  selector: selectors.getSelected,
});

export const useUser = createHook<State, Actions>(Store);

export const useUserSelected = createHook<
  State,
  Actions,
  UserSelectedState,
  void
>(Store, { selector: selectors.getSelected });
github atlassian / react-sweet-state / examples / advanced-scoped-flow / components / messages.js View on Github external
const actions = {
  add: (message: string) => ({ setState, getState }: StoreActionApi<State>) => {
    setState({
      data: [...getState().data, message],
    });
  },
};

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

export const MessagesSubscriber = createSubscriber<State, Actions>(Store);

export const useMessages = createHook<State, Actions>(Store);
github atlassian / react-sweet-state / examples / advanced-flow / components / todo / index.js View on Github external
const Store = createStore<State, Actions>({
  initialState,
  actions,
});

export const TodoContainer = createContainer<State, Actions, ContainerProps>(
  Store,
  {
    onUpdate: () => ({ dispatch }, { selectedUser }) => {
      if (selectedUser) dispatch(actions.load(selectedUser));
    },
  }
);

export const TodoSubscriber = createSubscriber<State, Actions>(Store);

export const useTodo = createHook<State, Actions>(Store);
github atlassian / react-sweet-state / examples / basic-flow / components.js View on Github external
};

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

const Store = createStore<State, Actions>({
  initialState,
  actions,
});

export const CounterSubscriber = createSubscriber<State, Actions>(Store);

export const useCounter = createHook<State, Actions>(Store);
github atlassian / react-sweet-state / examples / basic-ts / components.tsx View on Github external
};

const actions = {
  increment: () => ({ setState, getState }: StoreActionApi<State>) => {
    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 / form.js View on Github external
});

export const FormContainer = createContainer<State, Actions, ContainerProps>(
  Store,
  {
    onUpdate: () => ({ setState }, { remoteUsers }) => {
      setState({ toUsers: remoteUsers });
    },
  }
);

export const FormSubscriber = createSubscriber<State, Actions>(Store);

export const useForm = createHook<State, Actions>(Store);

export const FormActions = createSubscriber<State, Actions, void, void>(Store, {
  selector: null,
});

export const useFormActions = createHook<State, Actions, void, void>(Store, {
  selector: null,
});

react-sweet-state

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

MIT
Latest version published 5 days ago

Package Health Score

87 / 100
Full package analysis