-
Notifications
You must be signed in to change notification settings - Fork 0
fix: 更正按钮点击时,UI DOM展示state不一致问题,正确渲染list的长度,补充TS参数类型的定义 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,30 @@ | ||
| import React, { useState } from 'react'; | ||
| import React, { useState, useEffect } from 'react'; | ||
| import { layoutEmitter } from '@/utils/EventEmitter'; | ||
|
|
||
| export default ({ initNumber }) => { | ||
|
|
||
| const [state, setstate] = useState(initNumber); | ||
| interface ItemProps { | ||
| initNumber: number; | ||
| } | ||
|
|
||
| // 1. initNumber需要定义参数类型 | ||
| export default ({ initNumber = 0 }: ItemProps) => { | ||
| // 2. setstate 需要驼峰标识 | ||
| const [state, setState] = useState(initNumber); | ||
| const [flag, setFlag] = useState(true); | ||
| // 因为index中使用了当前组件,所以在index将元素挂载到DOM上都会调用当前组件的useEffect | ||
| // 而这时候EventEmitters中的this.subscriptions = subscription;并未执行,会报错 | ||
| // 这里设置了一个flag来控制第一次挂载到DOM不执行useEffect中的回调函数 | ||
| useEffect(() => { | ||
| if (!flag) { | ||
| layoutEmitter.emit({ state }); | ||
| } | ||
| }, [state]) | ||
| // 3.setState() 是异步更新,将layoutEmitter.emit({ state });放在button的点击事件中 | ||
| // 会一直拿不到最新的值。解决方法:通过useEffect来实现当state改变把最新的值传给layoutEmitter.emit({ state }) | ||
| return <button | ||
| style={{ fontSize: '30px' }} | ||
| onClick={() => { | ||
| setstate(state + 1) | ||
| layoutEmitter.emit({ state }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这删掉了,还怎么添加列表数据? |
||
| setState(state => state + 1) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 无意义修改,简写就行 |
||
| setFlag(false) | ||
| }} | ||
| >EventEmitter {state} </button> | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,35 @@ import React, { useEffect, useState } from 'react'; | |
| import { layoutEmitter } from '@/utils/EventEmitter'; | ||
| import EventEmitterButton from '@/components/EventEmitterButton'; | ||
|
|
||
| interface ItemProps { | ||
| state: number; | ||
| } | ||
|
|
||
| interface State { | ||
| state: number | ||
| } | ||
| export default () => { | ||
| const [list, setList] = useState([]); | ||
| const [list, setList] = useState([] as Array<State>); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 类型定义 |
||
|
|
||
| useEffect(() => { | ||
| layoutEmitter.useSubscription((data) => { | ||
| list.push(data); | ||
| console.log(data); | ||
| console.log(list); | ||
| setList(list); | ||
| // 4. list 为数组,引用数据类型,我们不能直接操作state要结构出来 | ||
| const listData = [...list] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 假象,明天的考核中会有体现 |
||
| setList(listData); | ||
| }); | ||
| }, []) | ||
|
|
||
| return ( | ||
| <div> | ||
| <EventEmitterButton initNumber={11} /> | ||
| <p>list length:{list.length}</p> | ||
| { | ||
| list.map(item => <p>{item.state}</p>) | ||
| // 5. item中state的参数类型 | ||
| // 6. 通过map循环需要给标签加唯一的key | ||
| list.map((item: ItemProps) => <p key={item.state}>{item.state}</p>) | ||
| } | ||
| </div> | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ class EventEmitter<T> { | |
| this.subscriptions(val); | ||
| }; | ||
|
|
||
| useSubscription = (callback: Subscription<T>) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 无意义修改,这个文件无错误。 |
||
| useSubscription = (callback: Subscription<any>) => { | ||
| function subscription(val: T) { | ||
| if (callback) { | ||
| callback(val); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
无意义,想太多了