简体   繁体   English

React hook,更新后直接访问 state 值

[英]React hook, accessing state value directly after updating it

I know this has been asked a lot and in many different ways but I couldn't find something that works for me.我知道这已经被问了很多,并且以许多不同的方式,但我找不到适合我的东西。 The functionality I want in my code is this我想要在我的代码中的功能是这个

const TestApp() => {
  const [count, setCount] = useState(null);
  const updateAndShowCount = () => {
    setCount(count + 1);
    // This console.log should be happen after setCount() has completed it's job
    console.log(count);
  }
}

The function will be called by a button onClick effect. function 将通过按钮 onClick 效果调用。 What it currently does is to update count and print the value of count before the update.它目前所做的是更新count并在更新之前打印count的值。

I want the button press to update count and immediately "after" print the updated value.我希望按下按钮来更新count并立即“在”打印更新后的值。

This is a test case for achieving what I want, in my actual project the function will receive a value from an input field and then make a get request using that value (with axios).这是一个实现我想要的测试用例,在我的实际项目中,function 将从输入字段接收一个值,然后使用该值(使用 axios)发出获取请求。 I am assuming that the answer to this simple test case will work in my case as well, and since this asynchronous behaviour is something I don't quite understand yet I thought it would be sensible to get a grasp of the general concept first.我假设这个简单测试用例的答案也适用于我的案例,并且由于这种异步行为是我不太了解的东西,但我认为首先掌握一般概念是明智的。

From what I have gathered though, I think I should mention that useEffect() is already being used for an initial get request, so if your answer involves using that you should assume that I don't know how to add something to useEffect() while retaining it's current functionality.从我收集到的信息来看,我想我应该提到useEffect()已经被用于初始获取请求,所以如果你的答案涉及使用它,你应该假设我不知道如何向useEffect()添加一些东西同时保留其当前功能。

The current useEffect() :当前的useEffect()

useEffect(() => {
  axios.get("mybackend/").then(res) => {
    setRequestData(res.data);
  });
}, []);

My actual question is in bold for clarity.为了清楚起见,我的实际问题以粗体显示。 Any info regarding the situation as a whole is welcome as well.也欢迎任何有关整体情况的信息。 Thanks for reading!谢谢阅读!

The solution follows naturally when adding count to your request (I guess it's some query parameter of some sort), because as you then use count inside the effect, it is a dependency of the effect, thus whenever the dependency changes, the effect will run again:当向您的请求添加count时,解决方案自然会遵循(我猜它是某种查询参数),因为当您在效果中使用count时,它是效果的依赖项,因此每当依赖项发生变化时,效果就会运行再次:

useEffect(() => {
  axios.get("mybackend?count=" + count).then(res) => {
   setRequestData(res.data);
  });
}, [count]);

Note that the effect is still missing a cleanup callback, which would cancel an ongoing request, so that when count changes again while the request is still ongoing, not two requests run at the same time concurrently.请注意,效果仍然缺少清理回调,这将取消正在进行的请求,因此当请求仍在进行时count再次更改时,不会有两个请求同时运行。

i'am not sure that i understood your question completely, but i think the answer is: you should use the callback in your setState, like that:我不确定我是否完全理解您的问题,但我认为答案是:您应该在 setState 中使用回调,如下所示:

setCount(count => count + 1)

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

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