【发布时间】:2019-09-24 20:31:05
【问题描述】:
我有一个 react-big-calendar,当我点击它时,它有一个按钮,他的模式会出现,我还有一个日历图标按钮,可以将我重定向到另一个 URL /planning。
我有一个对话框 (AjouterDisponibilite),我使用组件 Agenda 上的 ref 调用它。
日历图标有一个onClick 我尝试的事件:
this.props.history.push('/planning');
但是当我运行它并点击图标时,URL 是正确的,但我得到一个error,如下所示:
TypeError: Cannot read property 'handleAjouter' of null 和
Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method
刷新此页面后,它就可以工作了。
我的代码是:https://codesandbox.io/s/xw01v53oz
我该如何解决?
【问题讨论】:
-
Props 在 React 中是不可变的。不要那样改变它们。如果您想保留数据以供在组件中使用,请使用状态,或者当然,从父级传递下来。
-
@rrd 请问我该如何解决?你能检查我的沙箱吗?
-
在这种情况下,变异道具不是问题。
this.props.history指向 react-router 历史对象,调用push是使用 react-router 重定向的推荐方法之一。更多详情:reacttraining.com/react-router/web/api/history -
哪个文件/行会抛出错误?该错误意味着您尝试更新未安装组件的状态。这首先不是路由问题。
-
还没用过
ref。您可以改进指向ref问题的问题。 :-)
标签: javascript reactjs react-router-dom ref react-ref