-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3523 from MortezaMirjavadi/demo
Add zustand and redux-toolkit to the demo.
- Loading branch information
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |