Skip to content

通过ref调用条件判断的组件时,用this.refName获取不到子组件实例 #2777

Closed
@CYchenyan

Description

组件中条件判断两个子组件:
{ENV_TYPE === 'weapp' &&
<CMap
ref={e => this.mapContainer = e}
onFillStart={this.fillStart}
onLoadVehicles={this.loadVehicles}
step={this.props.step}
start={start}
terminate={terminate}
markers={this.state.markers}
order={updatedOrder}
previewOrderConfig={previewOrderConfig}
screenHeight={this.state.screenHeight}
/>
}

    {ENV_TYPE === 'h5' &&
      <H5Map
        ref={e => this.mapContainer = e}
        screenHeight={this.state.screenHeight}
        step={this.props.step}
        onFillStart={this.fillStart}
      />
    }

在方法中调用:
/**

  • 用户点击定位
    **/
    setMapCenterByUser = () => {
    console.log( this.mapContainer) // null
    this.mapContainer && this.mapContainer.setMapCenterByUserLocation(true)
    }
    如果在组件中不是条件判断显示子组件,该方法中能获取到this.mapContainer实例

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions