reactjs - 如何访问 LoadableComponent 和 Route 中的子组件?

标签 reactjs react-router react-loadable

在我的主要组件中,我创建了一个引用:

this.transfernewRef = React.createRef();

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

<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 对象:

enter image description here

如何从那里访问我的组件 TransferNew ?我似乎找不到合适的方法来做到这一点。我只想调用 TransferNew 的函数之一,所以类似的事情:

this.transfernewRef.current.<something>.myFunction()

谢谢。

最佳答案

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

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} />;
  },
});

关于reactjs - 如何访问 LoadableComponent 和 Route 中的子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677316/

相关文章:

reactjs - 使用文件协议(protocol)在本地访问 react 路由器路由?

reactjs - <Switch> 内特定路由的上下文提供者

reactjs - 代码分割/ react 可加载问题

reactjs - React 可加载导入连接组件

javascript - React - 从父级到子级访问函数参数

javascript - 在父组件获取 API 数据后同步子组件以接受 props

reactjs - 在所有路线上 react 加载屏幕?

reactjs - 从React中另一个不相关的组件调用一个组件的功能

android - 使用循环并渲染多个 jsx 项目,稍后返回渲染

javascript - 我如何将 react-loadable 与 react-router 一起使用?