Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/components/EventEmitterButton.tsx
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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无意义,想太多了

// 因为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 });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这删掉了,还怎么添加列表数据?

setState(state => state + 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无意义修改,简写就行

setFlag(false)
}}
>EventEmitter {state} </button>
};
18 changes: 15 additions & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

类型定义
useState()


useEffect(() => {
layoutEmitter.useSubscription((data) => {
list.push(data);
console.log(data);
console.log(list);
setList(list);
// 4. list 为数组,引用数据类型,我们不能直接操作state要结构出来
const listData = [...list]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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>
)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EventEmitter<T> {
this.subscriptions(val);
};

useSubscription = (callback: Subscription<T>) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无意义修改,这个文件无错误。

useSubscription = (callback: Subscription<any>) => {
function subscription(val: T) {
if (callback) {
callback(val);
Expand Down