|
| 1 | +--- |
| 2 | +id: component |
| 3 | +title: Component |
| 4 | +sidebar_label: Component |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | + |
| 10 | +## `registerComponent` |
| 11 | +Every screen component in your app must be registered with a unique name. The component itself is a traditional React component extending `React.Component` or `React.PureComponent`. It can also be a HOC to provide context (or a Redux store) or a functional component. Similar to React Native's `AppRegistry.registerComponent`. |
| 12 | + |
| 13 | +#### Parameters |
| 14 | +| Name | Required | Type | Description | |
| 15 | +| ------ | -------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------- | |
| 16 | +| componentName | Yes | string | Unique component name - not to be confused with **componentId** | |
| 17 | +| componentProvider | Yes | Function | Anonymous function that returns the React component to register, **OR** the component wrapped with Providers| |
| 18 | +| concreteComponentProvider | No | Function | Anonymous function that returns the concrete React component. If your component is wrapped with Providers, this must be an instance of the concrete component.| |
| 19 | + |
| 20 | +#### Examples |
| 21 | +##### Registering a component |
| 22 | +```js |
| 23 | +Navigation.registerComponent(`navigation.playground.WelcomeScreen`, () => WelcomeScreen); |
| 24 | +``` |
| 25 | + |
| 26 | +##### Registering a component wrapped with Providers |
| 27 | +```js |
| 28 | +import { Provider } from 'react-redux'; |
| 29 | +const store = createStore(); |
| 30 | + |
| 31 | +Navigation.registerComponent(`navigation.playground.MyScreen`, () => (props) => |
| 32 | + <Provider store={store}> |
| 33 | + <MyScreen {...props} /> |
| 34 | + </Provider>, |
| 35 | + () => MyScreen) |
| 36 | +); |
| 37 | +``` |
| 38 | + |
| 39 | +## `setLazyComponentRegistrator` |
| 40 | +Pass a function that will allow you to register a component lazily. When encountering an unknown component name, this function will be called, giving you a chance to register the component before an error is thrown. |
| 41 | + |
| 42 | +#### Parameters |
| 43 | +| Name | Required | Type | Description | |
| 44 | +| ------ | -------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------- | |
| 45 | +| lazyRegistratorFn | Yes | (lazyComponentRequest: string | number) => void | A function that takes a componentName, will be called when encountering an unknown componentName | |
| 46 | + |
| 47 | +#### Example |
| 48 | +```js |
| 49 | +Navigation.setLazyComponentRegistrator((componentName) => { |
| 50 | + if (componentName === 'navigation.playground.LazyScreen') { |
| 51 | + Navigation.registerComponent(Screens.LazilyRegisteredScreen, () => LazyScreen); |
| 52 | + } |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +## `updateProps` |
| 57 | +Update props of a component registered with [registerComponent](#registercomponent). Updating props triggers the component lifecycle methods related to [updating](https://reactjs.org/docs/react-component.html#updating). |
| 58 | + |
| 59 | +#### Parameters |
| 60 | +| Name | Required | Type | Description | |
| 61 | +| ----------- | -------- | -------- | ---------------------------------------------------------------------------------------------------------------------- | |
| 62 | +| componentId | Yes | string | Unique component id | |
| 63 | +| options | Yes | object | Props object to pass to the component | |
| 64 | +| callback | No | Function | Function that will be executed once inner `setState` is completed | |
| 65 | + |
| 66 | +#### Example |
| 67 | +```js |
| 68 | +Navigation.updateProps('MY_COMPONENT', { |
| 69 | + // props to pass to the component |
| 70 | +}; |
| 71 | +``` |
| 72 | +
|
| 73 | +:::important |
| 74 | +`updateProps` is called with a componentId. This is the same unique id components have in props. Don't confuse it with the `componentName` we use when registering components with [Navigation.registerComponent](#registerComponent). |
| 75 | +::: |
0 commit comments