Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 从实例化的React.component呈现React组件_Reactjs_Typescript - Fatal编程技术网

Reactjs 从实例化的React.component呈现React组件

Reactjs 从实例化的React.component呈现React组件,reactjs,typescript,Reactjs,Typescript,我有两个React组件,它们都基于同一个基类,这些组件有两个属性,我想在渲染组件之前阅读这些属性。这与其他地方使用的某些条件有关 目前,我正在调用一个方法,在我的Render函数中使用类似的方法 public getWidget(): JSX.Element { let widget = null; switch (widgetType) { case 'widget1': { widgetComponent = new Widget1(

我有两个React组件,它们都基于同一个基类,这些组件有两个属性,我想在渲染组件之前阅读这些属性。这与其他地方使用的某些条件有关

目前,我正在调用一个方法,在我的
Render
函数中使用类似的方法

public getWidget(): JSX.Element {

    let widget = null;
    switch (widgetType) {
        case 'widget1': {
            widgetComponent = new Widget1(props); // private variable in my class
            widget = (<Widget1 { ...props } ref = { some-ref });
        }
        case 'widget2': {
            widgetComponent = new Widget2(props); // private variable in my class
            widget = (<Widget2 { ...props } ref = { some-ref });
        }
    }
    return widget;
}
据我所知,我为React组件设置的引用仅在渲染后可用?否则我就用这个

我还尝试在自己的
render
方法中调用
this.widgetComponent.render()
,但这没有正确设置组件(可能是因为缺少
componentWillMount
componentDidMount
调用)

我简直不敢相信这是一种方法,在我的
render
方法中有没有一种方法可以从
this.widgetComponent
进行渲染,或者有没有一种方法可以从JSX.Element后面的类中获取属性


空检查和其他内容都从这些代码片段中删除:)

给小部件一个参考

widget = (<Widget1 { ...props } ref = { widget1 } />);
widget = (<Widget1 { ...props } ref = { widget1 } />);
componentDidMount(){
    const somethingIWantToKnow  = this.widget1.current.someProperty
}