MCPcopy
hub / github.com/kenberkeley/react-demo / updateState

Method updateState

src/components/Msg/MsgForm/index.js:45–69  ·  view source on GitHub ↗
({ location, params: { msgId }, userData: { username }, msg: { msgs } } = this.props)

Source from the content-addressed store, hash-verified

43
44 /* 不传入 props 则默认使用当前 props */
45 updateState ({ location, params: { msgId }, userData: { username }, msg: { msgs } } = this.props) {
46 // 情况1:处于 /msg/add,直接就是还原初始状态
47 if (isAddMode(location.pathname)) {
48 return this.setState(getInitState())
49 }
50
51 // 情况2:处于 /msg/modify/:msgId,且 state 中 msgs 不为空
52 if (msgs.length) {
53 let nextState = msgs.filter(({ id }) => id === msgId)[0]
54 if (!nextState || nextState.author !== username) {
55 return this.handleIllegal()
56 }
57 return this.setState(nextState)
58 }
59
60 // 情况3:强制刷新 /msg/detail/:msgId 后,跳转到 /msg/modify/:msgId
61 // 此时 state 中 msgs 为空,需要立即从后端 API 获取
62 msgService.fetch({ msgId }).then(msg => {
63 let { id, title, content, author } = msg
64 if (!msg || author !== username) {
65 return this.handleIllegal()
66 }
67 this.setState({ id, title, content })
68 })
69 }
70
71 handleIllegal () {
72 // 使用 setTimeout 防止阻塞跳转

Callers 2

componentDidMountMethod · 0.95

Calls 4

handleIllegalMethod · 0.95
isAddModeFunction · 0.85
getInitStateFunction · 0.85
fetchMethod · 0.80

Tested by

no test coverage detected