1
- import React from "react" ;
1
+ import React , { useEffect } from "react" ;
2
2
import { CodeBlock } from "@atlaskit/code" ;
3
+ import { useDispatch } from "react-redux" ;
3
4
4
5
function ReduxPage ( ) {
5
6
6
- const reduxInstall = `npm install -s react-redux @types/react-redux @reduxjs/toolkit`
7
+ const reduxInstall = `npm install -s react-redux @types/react-redux @reduxjs/toolkit`
7
8
8
- const reduxAppStore = `import notificationReducer from "./reducers/notificationReducer";
9
+ const reduxAppStore = `// state/appStore.ts
10
+ import notificationReducer from "./reducers/notificationReducer";
9
11
import {configureStore} from "@reduxjs/toolkit";
10
12
import stackReducer from "./reducers/stackReducer";
11
13
@@ -18,12 +20,13 @@ export const appStore = configureStore({
18
20
19
21
export type State = ReturnType<typeof appStore.getState>`
20
22
21
- const reduxRegister = `// index.tsx
23
+ const reduxRegister = `// index.tsx
22
24
<Provider store={appStore}>
23
25
<App/>
24
26
</Provider>`
25
27
26
- const reduxReducer = `import {Notification} from "../../model/AppModel";
28
+ const reduxReducer = `// state/reducers/notificationReducer.ts
29
+ import {Notification} from "../../model/AppModel";
27
30
28
31
const initialState: Notification[] = []
29
32
@@ -70,7 +73,7 @@ const notificationReducer = (state: Notification[] = initialState, action: Actio
70
73
71
74
export default notificationReducer`
72
75
73
- const reduxUsage = `// get updating notifications from stack (reloads on change)
76
+ const reduxUsage = `// get updating notifications from state (reloads component on state change)
74
77
const notifications = useSelector((appState: State) => appState.notifications)
75
78
76
79
// update state
@@ -82,47 +85,63 @@ dispatch({
82
85
type: 'ADD_NOTIFICATION',
83
86
notification: notification
84
87
})`
88
+ const dispatch = useDispatch ( )
89
+ useEffect ( ( ) => {
90
+ dispatch ( {
91
+ type : "SET_MENU"
92
+ } )
93
+ } , [ ] )
85
94
86
95
return (
87
96
< div >
88
97
< h1 > Redux</ h1 >
89
- < p > Redux is used for global stack handling. For example for handling notifications</ p >
90
-
91
- < h5 > Install Redux</ h5 >
92
- < p > Not needed if you use this library. It is already included</ p >
93
- < br />
94
- < CodeBlock
95
- language = "bash"
96
- text = { reduxInstall }
97
- />
98
-
99
- < h5 > Init appStore</ h5 >
100
- < br />
101
- < CodeBlock
102
- language = "typescript"
103
- text = { reduxAppStore }
104
- />
105
-
106
- < h5 > Integrate appStore</ h5 >
107
- < br />
108
- < CodeBlock
109
- language = "tsx"
110
- text = { reduxRegister }
111
- />
112
-
113
- < h5 > Create reducer</ h5 >
114
- < br />
115
- < CodeBlock
116
- language = "typescript"
117
- text = { reduxReducer }
118
- />
119
-
120
- < h5 > Use and change redux states</ h5 >
121
- < br />
122
- < CodeBlock
123
- language = "typescript"
124
- text = { reduxUsage }
125
- />
98
+ < p > Redux is used for global state handling. For example for handling notifications.</ p >
99
+
100
+ < div id = "dependencies" menu-name = "Dependencies" className = "menu pd" >
101
+ < h5 > Dependencies</ h5 >
102
+ < p > This library uses the following dependencies:</ p >
103
+ < br />
104
+ < CodeBlock
105
+ language = "bash"
106
+ text = { reduxInstall }
107
+ />
108
+ </ div >
109
+
110
+ < div id = "init-appstore" menu-name = "Init AppStore" className = "menu pd" >
111
+ < h5 > Init appStore</ h5 >
112
+ < br />
113
+ < CodeBlock
114
+ language = "typescript"
115
+ text = { reduxAppStore }
116
+ />
117
+ </ div >
118
+
119
+ < div id = "integrate-appstore" menu-name = "Integrate AppStore" className = "menu pd" >
120
+ < h5 > Integrate appStore</ h5 >
121
+ < br />
122
+ < CodeBlock
123
+ language = "tsx"
124
+ text = { reduxRegister }
125
+ />
126
+ </ div >
127
+
128
+ < div id = "create-reducer" menu-name = "Create Reducer" className = "menu pd" >
129
+ < h5 > Create reducer</ h5 >
130
+ < br />
131
+ < CodeBlock
132
+ language = "typescript"
133
+ text = { reduxReducer }
134
+ />
135
+ </ div >
136
+
137
+ < div id = "use-redux" menu-name = "Use Redux States" className = "menu pd" >
138
+ < h5 > Use and change redux states</ h5 >
139
+ < br />
140
+ < CodeBlock
141
+ language = "typescript"
142
+ text = { reduxUsage }
143
+ />
144
+ </ div >
126
145
</ div >
127
146
)
128
147
}
0 commit comments