Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* Add simple logger middleware
*/
const mw: Middleware = storeState => next => arg => {
/* eslint-disable no-console */
console.log(storeState.key, 'changing', arg);
const result = next(arg);
console.log(storeState.key, 'changed');
return result;
};
defaults.middlewares.add(mw);
/**
* Enable Redux devtools support
*/
defaults.devtools = true;
/**
* Main App
*/
class App extends Component<{}> {
render() {
return (
<div>
<h1>User Todos example</h1>
<main>
<div>
<h3>With Render-props</h3>
</div>
<hr></main></div>
import '@babel/polyfill';
import { UserListRpc, UserListHook } from './views/user-list';
import { TodoListRpc, TodoListHook } from './views/todo-list';
/**
* Add simple logger middleware
*/
const mw: Middleware = storeState => next => arg => {
/* eslint-disable no-console */
console.log(storeState.key, 'changing', arg);
const result = next(arg);
console.log(storeState.key, 'changed');
return result;
};
defaults.middlewares.add(mw);
/**
* Enable Redux devtools support
*/
defaults.devtools = true;
/**
* Main App
*/
class App extends Component<{}> {
render() {
return (
<div>
<h1>User Todos example</h1>
<main>
<div>
<h3>With Render-props</h3></div></main></div>
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(Store);
export const useTheme = createHook(Store);
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());
}
},
}
);
export const ThemeSubscriber = createSubscriber(Store);
export const useTheme = createHook(Store);
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());
}
},
{ 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());
}
},
}
);
export const ThemeSubscriber = createSubscriber(Store);
export const useTheme = createHook(Store);
// @flow
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { defaults } from 'react-sweet-state';
import '@babel/polyfill';
import { ChatRpc } from './views/chat-rpc';
import { ChatHook } from './views/chat-hook';
/**
* Enable Redux devtools support
*/
defaults.devtools = true;
/**
* Main App
*/
class App extends Component<
{},
{ reset: number, remount: number, remoteUsers: number }
> {
state = {
reset: 0,
remount: 0,
remoteUsers: 20,
};
reset = () => {
const reset = this.state.reset + 2;
type FilteredTodosState = $Call<
typeof getFilteredTodosSelector,
State,
TodosFilteredProps
>;
export const TodosFilteredSubscriber = createSubscriber<
State,
Actions,
FilteredTodosState,
TodosFilteredProps
>(Store, {
selector: getFilteredTodosSelector,
});
export const useTodosFiltered = createHook<
State,
Actions,
FilteredTodosState,
TodosFilteredProps
>(Store, {
selector: getFilteredTodosSelector,
});
/** Subscribers / Hooks */
const getAllTodosSelector = state => ({
data: state.order.map(v => state.byId[v]),
loading: state.loading,
});
export const TodosSubscriber = createSubscriber(
Store,
{
selector: getAllTodosSelector,
}
);
export const useTodos = createHook(Store, {
selector: getAllTodosSelector,
});
const getTodosCountSelector = state => ({ count: state.order.length });
export const TodosCountSubscriber = createSubscriber(
Store,
{
selector: getTodosCountSelector,
}
);
export const useTodosCount = createHook(Store, {
selector: getTodosCountSelector,
});
);
export const useTodos = createHook(Store, {
selector: getAllTodosSelector,
});
const getTodosCountSelector = state => ({ count: state.order.length });
export const TodosCountSubscriber = createSubscriber(
Store,
{
selector: getTodosCountSelector,
}
);
export const useTodosCount = createHook(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),
loading: state.loading,
});
type FilteredTodosState = $Call<
typeof getFilteredTodosSelector,
State,
TodosFilteredProps