应用于子组件时未设置useRef current

时间:2020-10-17 03:42:29

标签: javascript reactjs react-native

在返回上方,这两个引用钩子都定义为:

const containerRef = useRef(); const couponBaseBannerRef = useRef();

但是,我的couponBaseBannerRef仍然没有设置...这是一个2x的孩子,但是我知道我在做什么错。这是我的代码,其中标出了相关部分。

非常感谢您!

return (

<>

<View style={styles.container}>

<MapViewfollowUserLocation={true}zoomEnabled={true}showsUserLocation={true}style={styles.mapStyle}provider={PROVIDER_GOOGLE}customMapStyle={customData}initialRegion={{latitude: location ? location.latitude : 40.318394568204184,longitude: location ? location.longitude : -111.70952489745419,latitudeDelta: 0.1,longitudeDelta: 0.1,}}onPress={() => {containerRef.current.onMapClick();}}>

{couponBaseApi.data.couponBases? couponBaseApi.data.couponBases.map((report) =>

(<Markerkey={report.couponBaseID}coordinate={{latitude: report.latitude,longitude: report.longitude,}}tracksViewChanges={false}icon={getMarkerIcon(report.type)}onPress={(e) => {e.stopPropagation();

//couponBaseBannerRef.current.image =

report.companyURL;containerRef.current.onClickBase();

console.log(couponBaseBannerRef); //current is undefined

console.log(containerRef); //Returns as a full object}}>

</Marker>)): false}

</MapView>

<AppContainer ref={containerRef}> //working reference set here



<CouponBase

//Reference set here

ref={couponBaseBannerRef}title="WENDYS"image="https://reactnativecode.com/wp-content/uploads/2017/05/react_thumb_install.png"subTitle="Fires and stuff">

</CouponBase>

</AppContainer>

</View></>);

实际上,布局是

<Parent>

<Middle>

<Child/>

<Middle

<Parent/>

我正在尝试从孩子那里获得参考,但是它的当前属性报告为未定义:

Object {
  "current": undefined,
}

0 个答案:

没有答案