javascript - 如果需要如何加载数据? react /减少。无SSR

标签 javascript reactjs redux redux-thunk

是否可以根据需要加载数据?

例如:我第一次加载挑战列表时,我进入了详细信息页面。现在我将刷新页面并会出现类似挑战未定义的错误,因为仅在加载挑战列表时调用api

那么问题是如何只在 store 为空时再次调用 api?

谢谢!

最佳答案

你可以这样设置你的状态:

this.setState({
  challenges :data
});

当您导航到详细信息页面时,您可以将其作为 Prop 传递给子组件:

render{
  return(
    <Details challenges={this.state.challenges }/>
  );
}

在详细信息组件中,您可以访问数据

this.props.challenges

并将其存储在变量或您的组件状态中。 这样您就可以始终保留您的数据

关于javascript - 如果需要如何加载数据? react /减少。无SSR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882833/

相关文章:

javascript - 更改子节点的文本

javascript - 未捕获的类型错误 : Cannot read property 'push' of undefined (React-Router-Dom)

javascript - react : What would be a recommended way to cache state of the app in the browser to minimize the amount of requests to the backend?

reactjs - 在 React 中隐藏和显示外部组件

javascript - 我将如何以用户使用 Redux 编写的状态存储文本?

javascript - Redux 状态在窗口重新加载时重置(客户端)

javascript - xmlhttprequest 在 JS 显示模块模式中使用 Promise

javascript - 使用数据属性搜索

javascript - Selenium - 如何在 JS 测试文件中使用 JavascriptExecutor?

javascript - 更新 redux store 中的嵌套数据