Skip to content

Commit 9a0c14a

Browse files
committed
refactor: update redux-base
1 parent cf644bf commit 9a0c14a

File tree

1 file changed

+19
-15
lines changed
  • packages/redux-base/src/components

1 file changed

+19
-15
lines changed

packages/redux-base/src/components/App.tsx

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react'
2-
import { connect, Provider } from 'react-redux'
3-
import { Dispatch } from 'redux'
2+
import { useDispatch, useSelector, Provider } from 'react-redux'
43
import { createStore } from 'redux'
54
import styled from 'styled-components'
65

@@ -14,6 +13,7 @@ const reset = (num: number) => ({
1413
type: RESET,
1514
payload: num,
1615
})
16+
1717
type Action = ReturnType<typeof increment | typeof reset>
1818

1919
// Reducer
@@ -22,6 +22,7 @@ const initialCounterState = {
2222
count: 0,
2323
defaultCount: 0,
2424
}
25+
2526
export const reducer = (state = initialCounterState, action: Action) => {
2627
switch (action.type) {
2728
case INCREMENT: {
@@ -40,16 +41,16 @@ export const reducer = (state = initialCounterState, action: Action) => {
4041
// Store
4142
export const store = createStore(
4243
reducer,
43-
(window as any).__REDUX_DEVTOOLS_EXTENSION__ &&
44-
(window as any).__REDUX_DEVTOOLS_EXTENSION__()
44+
(window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__()
4545
)
4646

4747
// Component
4848
type Props = CounterState & {
4949
increment: () => Action
5050
reset: (num: number) => Action
5151
}
52-
const Component = (props: Props) => {
52+
53+
const Counter = (props: Props) => {
5354
return (
5455
<React.Fragment>
5556
<Layout>
@@ -62,26 +63,29 @@ const Component = (props: Props) => {
6263
</React.Fragment>
6364
)
6465
}
66+
6567
const Layout = styled.div`
6668
display: flex;
6769
justify-content: center;
6870
margin: 10px;
6971
`
7072

7173
// Container
72-
const mapStateToProps = (state: CounterState) => state
73-
const mapDispatchToProps = (dispatch: Dispatch) => ({
74-
increment: () => dispatch(increment()),
75-
reset: (num: number) => dispatch(reset(num)),
76-
})
77-
const AppComponent = connect(
78-
mapStateToProps,
79-
mapDispatchToProps
80-
)(Component)
74+
const counterSelector = (state: CounterState) => state
75+
76+
const CounterContainer = () => {
77+
const { count } = useSelector(counterSelector)
78+
const dispatch = useDispatch()
79+
80+
const inc = () => dispatch(increment())
81+
const res = (num: number) => dispatch(reset(num))
82+
83+
return <Counter defaultCount={0} count={count} increment={inc} reset={res} />
84+
}
8185

8286
// connect Store
8387
export const App = () => (
8488
<Provider store={store}>
85-
<AppComponent />
89+
<CounterContainer />
8690
</Provider>
8791
)

0 commit comments

Comments
 (0)