lab07 redux review · ui •use props rather then state today.jsx (lab-react-weathermood-post)...

14
Lab07 Redux Review WebApp DataLab, CS, NTHU 2019 Spring

Upload: others

Post on 02-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Lab07Redux Review

WebAppDataLab, CS, NTHU

2019 Spring

Page 2: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Why Redux?

• Separation of concerns

• Unidirectional (top-down) data flow in React

• States easy to inspect

• State changes reversible; easy to debug

Page 3: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Redux Workflow

Page 4: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

From React To Redux

Today.jsx(lab-react-weathermood-post)

Today.jsx(redux-weathermood-post)

Page 5: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

UI

• Use props rather then state

Today.jsx(lab-react-weathermood-post)

Today.jsx(redux-weathermood-post)

Page 6: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

UI

• Dispatch actions

Today.jsx(lab-react-weathermood-post)

Today.jsx(redux-weathermood-post)

Page 7: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Action

• You can dispatch multiple (asynchronous) actions in one action

weather-action.js

Page 8: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Action

• You can dispatch multiple (asynchronous) actions in one action

weather-action.js

Page 9: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Action

• In a simple action, return the type of action and parameters (which will be sent to reducer later)

weather-action.js

Page 10: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Reducer

• In a reducer, return the new state for redux store

weather-reducers.js

Page 11: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Reducer – Correct way to update state

• Wrong:• Modifying your state directly

• Correct:• Making copies of your data, and applying your changes to the copies• Use spread operator, or functions like Object.assign()

Page 12: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Connect Redux store to React

• Use connect function

• Pass a function to connect• Input: The state of redux store• Output: The props of the react component

Today.jsx

Page 13: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Back To UI

• Use the props of the react component to render

Today.jsx

Page 14: Lab07 Redux Review · UI •Use props rather then state Today.jsx (lab-react-weathermood-post) Today.jsx (redux-weathermood-post)

Assignment

(5%)