Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zustand and redux-toolkit to the demo. #3523

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions demo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import SuspenseRouterBug from './suspense-router';
import NestedSuspenseBug from './nested-suspense';
import Contenteditable from './contenteditable';
import { MobXDemo } from './mobx';
import Zustand from './zustand';
import ReduxToolkit from './redux_toolkit';

let isBenchmark = /(\/spiral|\/pythagoras|[#&]bench)/g.test(
window.location.href
Expand Down Expand Up @@ -135,6 +137,12 @@ class App extends Component {
<Link href="/contenteditable" activeClassName="active">
contenteditable
</Link>
<Link href="/zustand" activeClassName="active">
zustand
</Link>
<Link href="/redux-toolkit" activeClassName="active">
redux-toolkit
</Link>
</nav>
</header>
<main>
Expand Down Expand Up @@ -165,6 +173,8 @@ class App extends Component {
<SuspenseRouterBug path="/suspense-router" />
<NestedSuspenseBug path="/nested-suspense" />
<Contenteditable path="/contenteditable" />
<Zustand path="/zustand" />
<ReduxToolkit path="/redux-toolkit" />
</Router>
</main>
</div>
Expand Down
89 changes: 88 additions & 1 deletion demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@material-ui/core": "4.9.5",
"@reduxjs/toolkit": "^2.2.3",
"d3-scale": "^1.0.7",
"d3-selection": "^1.2.0",
"htm": "2.1.1",
Expand All @@ -26,7 +27,8 @@
"react-router": "^5.0.1",
"react-router-dom": "^5.0.1",
"redux": "^4.0.1",
"styled-components": "^4.2.0"
"styled-components": "^4.2.0",
"zustand": "^4.5.2"
},
"volta": {
"extends": "../package.json"
Expand Down
60 changes: 60 additions & 0 deletions demo/redux-toolkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { createElement } from 'preact';
import { Provider, useSelector } from 'react-redux';
import { configureStore, createSlice } from '@reduxjs/toolkit';

const initialState = {
value: 0
};
const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: state => {
state.value += 1;
},
decrement: state => {
state.value -= 1;
}
}
});
const store = configureStore({
reducer: {
counter: counterSlice.reducer
}
});

function Counter({ number }) {
const count = useSelector(state => state.counter.value);
return (
<div>
Counter #{number}:{count}
</div>
);
}

export default function ReduxToolkit() {
function increment() {
store.dispatch(counterSlice.actions.increment());
}
function decrement() {
store.dispatch(counterSlice.actions.decrement());
}
function incrementAsync() {
setTimeout(() => {
store.dispatch(counterSlice.actions.increment());
}, 1000);
}
return (
<Provider store={store}>
<div>
<h1>Redux Toolkit</h1>
<h1>Counter</h1>
<Counter number={1} />
<Counter number={2} />
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<button onClick={incrementAsync}>Increment Async</button>
</div>
</Provider>
);
}
53 changes: 53 additions & 0 deletions demo/zustand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createElement } from 'preact';
import create from 'zustand';

const useStore = create(set => ({
value: 0,
text: 'John',
setText: text => set(state => ({ ...state, text })),
increment: () => set(state => ({ value: state.value + 1 })),
decrement: () => set(state => ({ value: state.value - 1 })),
incrementAsync: async () => {
await new Promise(resolve => setTimeout(resolve, 1000));
set(state => ({ value: state.value + 1 }));
}
}));

function Counter({ number }) {
const value = useStore(state => state.value);
return (
<div>
Counter #{number}: {value}
</div>
);
}
function Text() {
const text = useStore(state => state.text);
const { setText } = useStore();
function handleInput(e) {
setText(e.target.value);
}
return (
<div>
Text: {text}
<input value={text} onInput={handleInput} />
</div>
);
}

export default function ZustandComponent() {
const { increment, decrement, incrementAsync } = useStore();

return (
<div>
<h1>Zustand</h1>
<h1>Counter</h1>
<Counter number={1} />
<Counter number={2} />
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<button onClick={incrementAsync}>Increment Async</button>
<Text />
</div>
);
}
Loading