简体   繁体   English

一旦 state 在反应中更改,useSelector 无法进行实时更新

[英]useSelector cannot make real time update once the state is changed in react

I'm implementing a search functionality by using react and redux thunk.我正在通过使用 react 和 redux thunk 来实现搜索功能。 The requirement for this search is to get fetchApi called in the thunk and update the state so we can display.此搜索的要求是在 thunk 中调用 fetchApi 并更新 state 以便我们可以显示。 The ui part is like this: ui部分是这样的:

function Search({fetchDataApi}) {
  const [data, setData] = useState([])
  const [token, setToken] = useState('')
  const dataFromState = useSelector(state=>state.newData) || []
  const handleSubmit = () => {
    fetchDataApi(token) // dispatch new data inside.It get updated by checking getState()
    setData(dataFromState)
  }
  const handleOnChange =(e)=> {
      setToken(e.target.value)
  }
  return (
    <div className="App">
        <input onChange={handleOnChange}/>
     <button onClick={handleSubmit}>Search</button>
     {data.map(item=><li>{item}</li>)}
     
    </div>
  );
}

export default App;

When I first click the button I got api called and state got update too, but useSelector won't return it in real time, so only empty array set by setData.当我第一次单击按钮时,我调用了 api 并且 state 也得到了更新,但是 useSelector 不会实时返回它,所以只有 setData 设置的空数组。 When I click the button second time, the data shows up.当我第二次单击按钮时,数据显示出来。 Is it something with the useSelector or other reason to cause this issue?是否与 useSelector 或其他原因导致此问题有关? This is the thunk action which I simplify:这是我简化的 thunk 动作:

const fetchDataApi = async(token, getState, dispatch) => {
...error handle
const response = await api(token)
dispatch(setNewData(response))
...console.log(getState()) // get updated successfully 
}

setNewData is a regular action which matches the reducer This is the setNewData: setNewData 是一个与 reducer 匹配的常规动作 这是 setNewData:

export const setNewData= (data)= ({
 type: 'SET_NEW_DATA'
 data
})

I think the issue is inside your handleSubmit function.我认为问题在于您的句柄提交 function。 You're calling setData when the 'dataFromState' may not be updated yet.当“dataFromState”可能尚未更新时,您正在调用 setData。

It also looks like that extra local state is unnecessary.看起来额外的本地 state 是不必要的。 I would map over dataFromState instead of keeping an exact copy around.我会 map over dataFromState 而不是保留精确的副本。 See below:见下文:

function Search({fetchDataApi}) {
  const [token, setToken] = useState('')
  const dataFromState = useSelector(state=>state.newData) || []
  const handleSubmit = () => {
    fetchDataApi(token) // dispatch new data inside.It get updated by checking getState()
  }
  const handleOnChange =(e)=> {
      setToken(e.target.value)
  }
  return (
    <div className="App">
        <input onChange={handleOnChange}/>
     <button onClick={handleSubmit}>Search</button>
     {dataFromState.map(item=><li>{item}</li>)}
     
    </div>
  );
}

export default App;

I think this post answer my question perfectly.我认为这篇文章完美地回答了我的问题。 redux state value changed at second click I resolve this issue by using useEffect. redux state 值在第二次单击时更改我使用 useEffect 解决了这个问题。 Thanks for the checking.感谢您的检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM