简体   繁体   English

反应导航 Stack.Screen 中的 {...props} 是什么?

[英]What is {…props} in react navigation Stack.Screen?

What is the significance of {...props} on this page ? 这个页面上的{...props}有什么意义?

<Stack.Screen name="Home">
  {props => <HomeScreen {...props} extraData={someData} />}
</Stack.Screen>

Without {...props} also, my app runs fine.如果没有{...props} ,我的应用程序运行良好。

Based on the docs description:根据文档描述:

Sometimes we might want to pass additional props to a screen.有时我们可能想要将额外的道具传递到屏幕上。

For passing additional props it uses a renderer (render callback) function.为了传递额外的道具,它使用渲染器(渲染回调)function。 actually, it is additional and obviously your codes should word without it.实际上,它是额外的,显然你的代码应该没有它。

The formal usage is like below:正式用法如下:

<Stack.Screen
  name="Home"
  component={HomeScreen}
  options={{ title: 'Overview' }}
/>

Als, in the formal usage React Navigation , applies optimizations to screen components to prevent unnecessary renders.此外,在正式使用React Navigation中,对屏幕组件应用优化以防止不必要的渲染。 I offer you to use this style, but if you need the additional props that the Stack.Screen would pass to the render component, use the renderer version but note using a renderer removes whole given optimizations by React Navigation .我建议您使用这种样式,但是如果您需要Stack.Screen将传递给渲染组件的其他道具,请使用渲染器版本,但请注意使用渲染器会删除React Navigation的整个给定优化。

If you wanna know what is the additional props, use the renderer version and then use console.log it in HomeScreen component:如果你想知道额外的道具是什么,请使用渲染器版本,然后在HomeScreen组件中使用console.log

const HomeScreen = props => {
  console.log(props);

  return (
    <View>
      <Text>Home Screen</Text>
    </View>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM