我在我的 React 应用程序中使用钩子来处理状态。
我的应用程序具有notes
与类别相关联的categoryId
.
NoteList
通过安装组件来显示特定类别中的注释。
<NoteList categoryObj={categoryObj} />
功能NoteList
组件
export const NoteList = (props) => {
const [notesToRender, setNotesToRender] = useState(props.notes)
useEffect(() => {
console.log('I fired!', props.categoryObj.id, props.notes)
setNotesToRender(props.notes)
}, [props.categoryObj.id]);
//...
return (
notesToRender.map((note) => {
return <NoteListItem key={note.id} {...note}/>
}
)
}
const mapStateToProps = (state) => {
notes: getNotesForCategory(state) // Selector that returns notes where note.id === props.categoryObj.id
}
export default connect(mapStateToProps)(NoteList)
选择器
export const getNotesForCategory = createSelector(
[getNotes, getcategoryId],
(notes, categoryId) => {
const categoryNotes = notes.filter(note => note.id === categoryId)
console.log('categoryNotes ', categoryNotes) // This log shows the correct notes
return categoryNotes
}
)
我创建notesToRender
是因为有时我需要在渲染注释之前过滤它们(此处未显示该代码)。
在类别(更新)之间切换时props.categoryObj.id
,注释无法正确显示。
加载应用程序时,状态是正确的,但在类别之间切换时,useEffect 会以某种方式导致状态“落后一步”。
- A 类 - 初始负载 - 正常
- 切换到“B 类”——console.log() 显示正确的 categoryId,但来自 A 类的注释
- 切换到“A 类”——console.log() 显示正确的 categoryId,但来自 B 类的注释
- 切换到“B 类”——console.log() 显示正确的 categoryId,但来自 A 类的注释
... 等等
选择器的控制台日志显示了正确的注释。问题可能是在useEffect之后mapStateToProps
触发?如何确保在 useEffect之前触发而不导致不必要的重新渲染 === 仅在更改时?mapStateToProps
props.categoryObj.id
亲切的问候/K