react-native-navigation的迁移库

docs-functional-components.mdx 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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().bindComponent(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. Notice that in the example above, we call `Navigation.events().bindComponent()` to register our listener even though we're not binding any component. That's because our listener is registered with the `componentId` passed in the second argument.
  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. :::