|
1 | | -import React, { ComponentType, FunctionComponent, useEffect } from 'react'; |
| 1 | +import React, { ComponentType, Component as ReactComponent } from 'react'; |
| 2 | +import { ViewStyle, View, StyleSheet } from 'react-native'; |
| 3 | +import { observer } from 'mobx-react/native'; |
2 | 4 | import { Canal } from './Canal'; |
3 | | -import { useNavigation } from './NavigationContext'; |
| 5 | +import { Navigation } from './Navigation.store'; |
4 | 6 |
|
5 | | -export const createCanal = (...PagesList: ComponentType[]): FunctionComponent => { |
| 7 | +interface ILocalTransitionerProps { |
| 8 | + style?: ViewStyle; |
| 9 | +} |
| 10 | + |
| 11 | +export const createCanal = ( |
| 12 | + ...PagesList: ComponentType[] |
| 13 | +): ComponentType<ILocalTransitionerProps> => { |
6 | 14 | for (let index = 0; index < PagesList.length; index++) { |
7 | 15 | const Page = PagesList[index]; |
8 | 16 | if (!(React.isValidElement(Page) || typeof Page === 'function')) { |
9 | 17 | throw new Error( |
10 | | - `\`createCanal\` expects its first arguments to be a React component. Received type for argument ${index + |
| 18 | + `\`createCanal\` expects its arguments to be React components. Received type for argument ${index + |
11 | 19 | 1}: ${typeof Page}` |
12 | 20 | ); |
13 | 21 | } |
14 | 22 | } |
15 | | - const LocalTransitioner = () => { |
16 | | - const FirstPage = PagesList[0]; |
17 | | - const navigation = useNavigation(); |
18 | | - useEffect(() => { |
19 | | - navigation.canalsSubject.next(new Canal(PagesList)); |
20 | | - }, [false]); |
21 | | - return <FirstPage />; |
22 | | - }; |
| 23 | + |
| 24 | + @observer |
| 25 | + class LocalTransitioner extends ReactComponent<ILocalTransitionerProps> { |
| 26 | + canal = new Canal(PagesList); |
| 27 | + |
| 28 | + constructor(props: ILocalTransitionerProps) { |
| 29 | + super(props); |
| 30 | + const navigation = Navigation.getInstance(); |
| 31 | + navigation.canalsSubject.next(this.canal); |
| 32 | + } |
| 33 | + |
| 34 | + render() { |
| 35 | + return ( |
| 36 | + <View style={this.props.style}> |
| 37 | + {Navigation.getInstance().state[this.canal.id].map(({ Component, name }) => ( |
| 38 | + <View style={StyleSheet.absoluteFill} key={name}> |
| 39 | + <Component /> |
| 40 | + </View> |
| 41 | + ))} |
| 42 | + </View> |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
23 | 46 | return LocalTransitioner; |
24 | 47 | }; |
0 commit comments