【问题标题】:How to access a child within LoadableComponent and Route?如何在 LoadableComponent 和 Route 中访问子级?
【发布时间】:2018-06-04 09:25:37
【问题描述】:

在我的主要组件中,我创建了一个参考:

this.transfernewRef = React.createRef();

并将其分配给我的组件,该组件嵌入在 Route 中:

<Route path="/new" render={(props) => <TransferNew {...props} origin={this.state.origin} token={this.state.token} ref={this.transfernewRef} />}/>

我的组件 TransferNew 也是使用 react-loadable 及其 Loadable 组件动态加载的。如果我打印 this.transfernewRef.current,我会得到一个 LoadableComponent 对象:

如何从那里访问我的组件 TransferNew ?我似乎找不到合适的方法来做到这一点。我只是想调用一个 TransferNew 的函数,所以按照这些思路:

this.transfernewRef.current.&lt;something&gt;.myFunction()

谢谢。

【问题讨论】:

  • 我也有同样的问题。有什么想法吗?

标签: reactjs react-router react-loadable


【解决方案1】:

我也有同样的问题。我的解决方案

import Loadable from "react-loadable";
import { Loading } from "components/Loading";

const Dropzone = Loadable({
  loader: () => import("view/components/knowledge/components/Dropzone.jsx"),
  loading: Loading,
  render(loaded, props) {
    const Component = loaded.default;
    const ref = props.DropzoneRef || undefined;
    return <Component {...props} ref={ref} />;
  },
});

【讨论】:

    猜你喜欢
    • 2015-06-16
    • 2021-12-14
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 2015-07-29
    • 2017-07-17
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多