Function is missing in type but required in type 'Props'

huangapple go评论77阅读模式
英文:

Function is missing in type but required in type 'Props'

问题

Here's the translated code:

我正在将一个函数传递给我的 `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>
);

在我的 `MapComponent` 它显示为 `undefined`并且在 TypeScript 中出现了上述错误我如何将我的函数传递给 `MapComponent`

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} // 默认值 => 0
        maxZoomLevel={15}
        provider={PROVIDER_GOOGLE}
        style={styles.map}>
        {Markers}
      </MapView>
      {showCard && <SearchResultCard {...resultProps} />}
    </View>
  );
};

export default MapComponent;

I've translated the code as requested.

英文:

I am passing a function to my MapComponent:

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

In my MapComponent, it is appearing as undefined, with the above error in TypeScript, How can I pass my function into my MapComponent?

type Props = {
results: SearchResult[];
onDirectionsPress: (
latitude: number,
longitude: number,
sitename: string,
) =&gt; void;
};
const MapComponent = ({ results, onDirectionsPress }: Props) =&gt; {
console.log(&#39;here&#39;, results, onDirectionsPress); &lt;&lt;&lt; undefined 
return (
&lt;View style={styles.container}&gt;
&lt;MapView
ref={mapRef}
minZoomLevel={2} // default =&gt; 0
maxZoomLevel={15}
provider={PROVIDER_GOOGLE}
style={styles.map}&gt;
{Markers}
&lt;/MapView&gt;
{showCard &amp;&amp; &lt;SearchResultCard {...resultProps} /&gt;}
&lt;/View&gt;
);
};
export default MapComponent;

答案1

得分: 3

你应该使用onDirectionsPress={onDirectionsPress}代替{...onDirectionsPress}来传递onDirectionsPress

<MapComponent results={results} onDirectionsPress={onDirectionsPress} />
英文:

You should be passing onDirectionsPress with onDirectionsPress={onDirectionsPress} instead of {...onDirectionsPress}:

&lt;MapComponent results={results} onDirectionsPress={onDirectionsPress} /&gt;

huangapple
  • 本文由 发表于 2023年5月25日 20:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定