wiluxy 最近的时间轴更新
wiluxy

wiluxy

V2EX 第 368932 号会员,加入于 2018-12-08 01:17:13 +08:00
今日活跃度排名 4151
wiluxy 最近回复了
1 天前
回复了 ooo4 创建的主题 React 请教一个关于 useEffect 依赖的问题
@chesha1 useMemo 、useEffect 漏依赖的问题,可以装个 eslint 插件(@eslint-react/eslint-plugin
)辅助查看依赖有没有问题,遵循 hooks 规则写下去,等以后 react compiler 完善自动优化吧,现在没有性能问题就不用管太多了,只要功能正常、没有性能问题,re-render 不是什么大事情,老是想着最佳实践很累的。
1 天前
回复了 ooo4 创建的主题 React 请教一个关于 useEffect 依赖的问题
@ooo4 如果不加这个 currentChatId 可以满足你的功能,非要用 useEffect 的话,可以考虑一下用 useReducer 组织状态

```typescript
import React, {useEffect, useReducer, useState} from "react"


type Conversation = {
id:string
title:string
[key:string]:unknown
}

const initialState:Conversation[] = [
{id:'1',title:"title-1"},
{id:'2',title:"title-2"},
{id:'3',title:"title-3"},
]

function App() {
const [currentChatId,setCurrentChatId] = useState("")
const [currentChatTitle,setCurrentChatTitle] = useState("")

const [conversation,setConversation] = useReducer<
React.Reducer<
Conversation[],
(prev:Conversation[],curChatId:typeof currentChatId)=>Conversation[]
>
>((prev,action)=>{
console.log(123)
return action(prev,currentChatId)
},initialState)

useEffect(()=>{
if(currentChatTitle){
setConversation((prev,curChatId)=>{
console.log({curChatId})
return prev.map(i=>{
return {
...i,
title: i.id === curChatId ? currentChatTitle : i.title,
}
})
})
}
},[currentChatTitle])

return <div>
<label htmlFor="currentChatId">currentChatId:</label>
<input type="text" id="currentChatId" value={currentChatId} onChange={(e)=>setCurrentChatId(e.target.value)} />
<label htmlFor="currentChatTitle">currentChatTitle:</label>
<input type="text" id="currentChatTitle" value={currentChatTitle} onChange={(e)=>setCurrentChatTitle(e.target.value)} />
<ul>
{
conversation.map(i=>{
return <li key={i.id}>{i.title}</li>
})
}
</ul>
</div>
}

export default App;


```
1 天前
回复了 ooo4 创建的主题 React 请教一个关于 useEffect 依赖的问题
典型的滥用 useEffect 的例子,@MossFox 的方案是对的,至于 @chesha1 说的不推荐 useMemo ,这里 useMemo 的作用类似于派生状态,心智负担比用 useEffect 小
那必须是这个
https://overreacted.io/

特别是这篇 useEffect 相关的
https://overreacted.io/a-complete-guide-to-useeffect/
12 天前
回复了 nikelei 创建的主题 职场话题 28 岁大龄 javaer,失业中迷茫中
@kandaakihito 样本不够,拿几个例子就总结出规律我觉得不太准确,我身边也有三本毕业一年,前端 12k 的案例
13 天前
回复了 nikelei 创建的主题 职场话题 28 岁大龄 javaer,失业中迷茫中
28 岁就拿大龄说事,自己先踩自己一脚,该你失业的。
看到这个帖子瞬间不高兴了,呜呜呜呜
39 天前
回复了 AokiNet 创建的主题 职场话题 年底裁员大礼包
分为几种情况
1.协商离职,赔偿金不给都行,商量好就可以。
2.合法裁员,具体怎么样才符合你可以自己去搜(你公司这个情况 99%不是合法裁员),这种情况给 n+1 ,这个 1 是代通知金。
3.违法裁员,可以要 2n ,但是公司一般不会直接给,你可以仲裁,如果你受了委屈想拿回应得的,那可以花点时间仲裁。
46 天前
回复了 yespee 创建的主题 职场话题 被裁了,但开心不起来
1. n 不满半年按照半年算,不满一年按一年算。

2. 如果你工作时间超过一年以上,闹麻了你就认定他非法裁员要 2n ,反正不能好聚好散,争取最大利益。

3. 你说这 10 分钟是上厕所也行,如果一直以来公司都没提醒过你,现在拿这个事情说事,好像是不支持的,具体要看当地司法实践。
71 天前
回复了 giserman001 创建的主题 剧集 兄弟们,求剧(动漫)
girl band cry 和 mygo
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1547 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 21ms · UTC 16:51 · PVG 00:51 · LAX 08:51 · JFK 11:51
Developed with CodeLauncher
♥ Do have faith in what you're doing.