ComponentDidMount 操作超时
ComponentDidMount actions with timeout
我正在尝试在我的组件挂载时执行一些操作,但不是立即执行。我的组件看起来像这样:
object MyComponent {
def apply = compopnent()
class Backend($: BackendScope) {
def render = {
// Some markup
}
def actions() = setTimeout(1000) {
//... Some state modifications
}
}
val component = ScalaComponent.builder[Unit]("My component")
.renderBackend[Backend]
.componentDidMount(f => f.backend.actions()) // HERE!
.build
}
我发现类型不匹配。找到 SetTimeoutHandle,需要 react.Callback.
如何在 componentDidMount 中使用超时?
CallbackTo
class 有 async
/ delay
/ delayMs
方法。您可以获得延迟状态 mod 回调,如下所示:$.modState(...).delayMs(1000).void
.
请注意,React.js 中的异步回调需要小心处理。挂载后 1 秒,您的组件可能已经卸载(理论上),如果您的回调在它已经卸载时运行,您将收到错误消息。在这方面,我不确定 scalajs-react 是否提供了 React.js 之上的任何东西。
我正在尝试在我的组件挂载时执行一些操作,但不是立即执行。我的组件看起来像这样:
object MyComponent {
def apply = compopnent()
class Backend($: BackendScope) {
def render = {
// Some markup
}
def actions() = setTimeout(1000) {
//... Some state modifications
}
}
val component = ScalaComponent.builder[Unit]("My component")
.renderBackend[Backend]
.componentDidMount(f => f.backend.actions()) // HERE!
.build
}
我发现类型不匹配。找到 SetTimeoutHandle,需要 react.Callback.
如何在 componentDidMount 中使用超时?
CallbackTo
class 有 async
/ delay
/ delayMs
方法。您可以获得延迟状态 mod 回调,如下所示:$.modState(...).delayMs(1000).void
.
请注意,React.js 中的异步回调需要小心处理。挂载后 1 秒,您的组件可能已经卸载(理论上),如果您的回调在它已经卸载时运行,您将收到错误消息。在这方面,我不确定 scalajs-react 是否提供了 React.js 之上的任何东西。