简体   繁体   English

为什么不能在函数中访问更新的prop值?

[英]Why can't I access the updated prop value within my function?

So I have a functional component that gets data from redux. 所以我有一个功能组件,可以从redux获取数据。 My issue is, I'm not able to access the current value of a certain prop, though the data is changing correctly in the redux store so it isn't an issue with redux, I don't think. 我的问题是,尽管数据在Redux存储中正确更改,但我无法访问某个道具的当前值,所以我认为Redux并不是问题。

I rewrote a simple version of my component below. 我在下面重写了我组件的一个简单版本。 Oddly enough, even returning the value .toString() in my component jsx returns the correct value so I'm hoping this is just some scope issue or something I'm not getting about my code that is not getting me the correct value. 奇怪的是,即使在我的组件jsx中返回值.toString()也会返回正确的值,所以我希望这只是一个范围问题,或者是我没有得到我的代码而没有得到正确的值。

const Player = ({ isPlaying }) => {

  function setProgress() {
    console.log(isPlaying) // returns false here continuously!
  }

  function playSong() {
    setInterval(() => {
      setProgress();
    }, 500);
  }

  useEffect(() => {
    console.log(isPlaying) // returns true here!
  }, [isPlaying])
}


const mapStateToProps = ({ isPlaying }) => {
  console.log(isPlaying); // also returns true here!
  return { isPlaying };
};

export default connect(mapStateToProps, null)(Player);


Here is a link to a CodeSandbox recreation of the problem I have. 这是我遇到的问题的CodeSandbox重新创建的链接。

https://codesandbox.io/s/playererrordemo-r6bt2 https://codesandbox.io/s/playererrordemo-r6bt2

setInterval creates a closure over isPlaying , that's why it's always the same value. setIntervalisPlaying之上创建一个闭包,这就是为什么它总是相同的值。

The interval keeps the reference to the value of isPlaying , it was called with. 时间间隔保留对isPlaying的值的引用,它是通过with调用的。 To change that you might need to clearInterval and start a new one when isPlaying changes. 要进行更改,您可能需要clearInterval并在isPlaying更改时开始一个新的isPlaying

暂无
暂无

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

相关问题 我似乎无法从这个函数中访问我更新的状态 - I can’t seem to access my updated state from within this function 为什么我不能在事件处理程序中访问我的功能组件的道具? - Why can't I access my functional component's props within an event handler? 子属性值未在Window上更新,但我可以看到它在控制台中得到更新 - Child prop value not updated on Window , but i can see it get updated in console 使用作为道具传递的函数对打字稿做出反应,但无法访问 - react typescript with function passed as prop, but can't access 我可以在组件中访问的道具无法呈现 - Prop I can access in component can't be rendered 不明白为什么我不能访问计数器的值 - Can't understand why I can't access the value of counter 为什么当我调用函数“StopTimer”函数时我的变量“intervalId”没有更新值? - Why my variable "intervalId" is not getting the value updated when I call the function "StopTimer" function? 当消费者调用时,作为上下文值传递的函数无法访问更新的组件状态 - Function passed as context value can't access updated component state when called by a consumer 为什么在测试中我的子组件上的道具未更新? - Why is the prop not being updated on my child component in my test? 为什么没有超时就无法访问componentDidMount中更新的redux数据? - Why can't I access updated redux data in componentDidMount without timeout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM