react-native-navigation的迁移库

docs-functional-components.mdx 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ---
  2. id: functionalComponents
  3. title: Using functional components as screens
  4. sidebar_label: Using functional components
  5. ---
  6. Both class components and functional components can be used as screens. There is no performance difference between the two. Deciding which type component to use boils down to user preference.
  7. ## Declaring static options
  8. Static options are a great way to declare options close to where they are used. Declaring static options is done by first declaring the functional component, and then declaring the options on the function class.
  9. ```jsx
  10. const MyScreen = (props) => {
  11. return (
  12. <View>
  13. <Text>Screen Component</Text>
  14. </View>
  15. );
  16. };
  17. MyScreen.options = {
  18. topBar: {
  19. background: {
  20. color: 'red'
  21. }
  22. }
  23. }
  24. ```
  25. ## Listening to navigation events
  26. Navigation events let you react to various events related to a specific component or the app its self. Read more about them [here](/react-native-navigation/api/events)
  27. ```jsx
  28. import { useEffect } from 'react';
  29. import Navigation from 'react-native-navigation';
  30. function MyScreen(props) {
  31. useEffect(() => {
  32. const listener = {
  33. componentDidAppear: () => {
  34. console.log('RNN', `componentDidAppear`);
  35. },
  36. componentDidDisappear: () => {
  37. console.log('RNN', `componentDidDisappear`);
  38. }
  39. };
  40. // Register the listener to all events related to our component
  41. const unsubscribe = Navigation.events().registerComponentListener(listener, props.componentId);
  42. return () => {
  43. // Make sure to unregister the listener during cleanup
  44. unsubscribe.remove();
  45. };
  46. }, []);
  47. return (
  48. <View>
  49. <Text>Screen Component</Text>
  50. </View>
  51. );
  52. }
  53. ```
  54. In the example above, our listener is registered with the `componentId` passed in the second argument, and will receive only its related events.
  55. :::tip
  56. [underscopeio/react-native-navigation-hooks](https://github.com/underscopeio/react-native-navigation-hooks) is a wonderful library which greatly simplifies usage with hooks by introducing dedicated hooks for each event. The following example, which is taken from their docs, shows how to listen to all appear events and a particular screen's appear events:
  57. ```jsx
  58. import { useNavigationComponentDidAppear } from 'react-native-navigation-hooks'
  59. const ScreenComponent = ({ componentId }) => {
  60. // Global listener
  61. useNavigationComponentDidAppear(e => {
  62. console.log(`${e.componentName} (${e.componentId}) appeared`)
  63. })
  64. // Listen events only for this screen (componentId)
  65. useNavigationComponentDidAppear(e => {
  66. console.log(`${e.componentName} appeared`)
  67. }, componentId)
  68. return (
  69. <View>
  70. <Text>Screen Component</Text>
  71. </View>
  72. )
  73. }
  74. ```
  75. :::