我找到了这个答案,但我无法用它解决问题。
我尝试在 Storybook 中加载的组件取决于通过 index.html 中的 CDN 注入的全局变量。
为了访问组件中的变量,我在文件顶部添加了此注释
/*global <variable name>*/
Run Code Online (Sandbox Code Playgroud)
它在 React 应用程序中工作正常,但 Storybook 抛出此错误:<variable name> is not defined
。
我尝试在文件顶部stories.js
和配置文件顶部添加该注释。- 没有运气。
CDN 的 URL 位于 env 文件中。通过查看此文档,我尝试在 HTML 文件中添加第二个脚本,其中包含一个前缀为STORYBOOK_
. 那也没用。
关于我能做些什么来克服这个问题有什么想法吗?
我正在尝试实现一些需要监听位置的东西。
useEffect(() => {
props.history.listen(location => {
// do stuff here
});
return () => {
props.history.listen(location => {
// now stop doing stuff
});
};
}, []);
Run Code Online (Sandbox Code Playgroud)
我已经看到了参考,unlisten()
但它没有得到很多赞成票,如果我这样做了,console.log(props.history.unlisten())
它会作为not a function
这是在 App.js 中完成的。我假设应该清理它,但我不确定它是否真的需要清理。
需要清理吗?如果是这样,正确的做法是什么?