代码之家  ›  专栏  ›  技术社区  ›  Bomber

类型中缺少函数,但类型“Props”中需要该函数

  •  3
  • Bomber  · 技术社区  · 2 年前

    我正在将函数传递给 MapComponent 以下为:

    type Props = {
      results: SearchResult[];
      ...
      onDirectionsPress: (
        latitude: number,
        longitude: number,
        sitename: string,
      ) => void;
    };
    
    const MapTab = ({
      results,
      fuelType,
      searchDistance,
      addressName,
      onDirectionsPress,
    }: Props) => (
      <View style={styles.container}>
        ...
        <MapComponent results={results} {...onDirectionsPress} />
      </View>
    );
    

    在我的 地图组件 ,显示为 undefined ,在TypeScript中出现上述错误时,我如何将函数传递到 地图组件 ?

    type Props = {
      results: SearchResult[];
      onDirectionsPress: (
        latitude: number,
        longitude: number,
        sitename: string,
      ) => void;
    };
    
    const MapComponent = ({ results, onDirectionsPress }: Props) => {
    
      console.log('here', results, onDirectionsPress); <<< undefined 
    
      return (
        <View style={styles.container}>
          <MapView
            ref={mapRef}
            minZoomLevel={2} // default => 0
            maxZoomLevel={15}
            provider={PROVIDER_GOOGLE}
            style={styles.map}>
            {Markers}
          </MapView>
          {showCard && <SearchResultCard {...resultProps} />}
        </View>
      );
    };
    
    
    
    export default MapComponent;
    
    1 回复  |  直到 2 年前
        1
  •  3
  •   Youssouf Oumar    2 年前

    你应该路过 onDirectionsPress 具有 onDirectionsPress={onDirectionsPress} 而不是 {...onDirectionsPress} 以下为:

    <MapComponent results={results} onDirectionsPress={onDirectionsPress} />