我有一个useEffect
Sub Test()
Dim fRowoffilterqcdata As Integer, lRowoffilterqcdata As Integer, lColoffilterqcdata As Integer
fRowoffilterqcdata = Cells(1, 8).End(xlDown).Row
lRowoffilterqcdata = Get_lRow(ActiveSheet)
lColoffilterqcdata = Cells(fRowoffilterqcdata, Columns.Count).End(xlToLeft).Column
Dim FilteredQCDataRng As Range
Set FilteredQCDataRng = Range(Cells(fRowoffilterqcdata, 1), Cells(lRowoffilterqcdata, lColoffilterqcdata))
FilteredQCDataRng.Select ' This Works
FilteredQCDataRng.Sort key1:="Method", key2:="Number", Order1:=xlDescending, Header:=xlYes ' This Failed
'Call FilterWorksheetHeaderRow(ActiveSheet, fRowoffilterqcdata)
End Sub
现在搜索实际上将更新该function Component () {
const facets = useSelector(getFacets)
useEffect(() => {
dispatch(search(facets, param2, param3))
}, [dispatch, facets, param2, param3])
//rest of component with return
}
facets
问题是,当运行useEffect时,重新渲染会导致//Inside my slice
const search = (facets, param2, param3) => async(dispatch) => {
//do a fetch using param2 and param3
//fetch comes back with a facets object
//alter response facets object based on 'old' facets
dispatch(setFacets(newFacets)) // set the new facets
}
加载新值... Linting要求我将const facets = useSelector(getFacets)
作为facets
中的依赖项,但是把它放在那里意味着它会一遍又一遍地循环。如何停止?
我尝试将逻辑从useEffect中提取出来,并将其放入useCallback中,但是再次,linting要求我将useEffect
作为依赖项,这基本上是相同的问题。
答案 0 :(得分:1)
Sooooo Redux有几种存储方法; dispatch
是其中之一。对我来说,我正在寻找的存储方法是getState。更多信息if you look here和cmd / ctrl + F'getState'
我认为我无法访问切片中的状态,因为出于明显的原因,React不允许我在切片中使用useSelector
。当时对我来说并不明显的是,我可以使用getState
facets
参数,因为我不再需要它了。function Component () {
//const facets = useSelector(getFacets) --> NO LONGER NEEDED
useEffect(() => {
dispatch(search(/*facets, */param2, param3))
}, [dispatch, /*facets, */param2, param3])
//rest of component with return
}
facets
参数,并在getState
旁边加入dispatch
//Inside my slice
const search = (param2, param3) => async(dispatch, getState) => {
//do a fetch using param2 and param3
//fetch comes back with a facets object
//grab 'old' facets using NEW getState and same selector: getFacets
oldFacets = getFacets(getState())
//alter response facets object based on oldFacets
dispatch(setFacets(newFacets)) // set the new facets
}