Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit 2743654

Browse files
committed
♻️ (NavigationContext) Use new context api instead of mobx-react
1 parent 752c8c2 commit 2743654

10 files changed

Lines changed: 53 additions & 47 deletions

example/src/App.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import React, { Component } from 'react';
2-
import { NavigationProvider } from 'react-gondola';
32
import { SignIn } from './canals/SignIn';
43

54
interface IProps {}
65
export default class App extends Component<IProps> {
76
render() {
8-
return (
9-
<NavigationProvider>
10-
<SignIn />
11-
</NavigationProvider>
12-
);
7+
return <SignIn />;
138
}
149
}

src/NavigationContext.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createContext } from 'react';
2+
import { Navigation } from './Navigation.store';
3+
4+
export const NavigationContext = createContext(new Navigation());

src/NavigationProvider.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import TestRenderer from 'react-test-renderer';
3+
import { View } from 'react-native';
4+
5+
import { NavigationContext } from '../NavigationContext';
6+
import { Navigation } from '../Navigation.store';
7+
8+
describe('NavigationContext', () => {
9+
it('initializes a context with a new navigation store', () => {
10+
const expectedNavigation = new Navigation();
11+
TestRenderer.create(
12+
<NavigationContext.Consumer>
13+
{navigation => {
14+
expect(navigation).toEqual(expectedNavigation);
15+
return <View />;
16+
}}
17+
</NavigationContext.Consumer>
18+
);
19+
expect.assertions(1);
20+
});
21+
});

src/__tests__/NavigationProvider.test.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/__tests__/__snapshots__/NavigationProvider.test.tsx.snap

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/__tests__/createCanal.test.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { createCanal } from '../createCanal';
77
describe('createCanal', () => {
88
it('throws an error if first arg is not a Component', () => {
99
try {
10+
// @ts-ignore
1011
createCanal('aaa');
1112
} catch (error) {
1213
expect(error.message).toBe(
13-
'`createCanal` expects its first argument to be a React component. Received type: string'
14+
'`createCanal` expects its first arguments to be a React component. Received type for argument 1: string'
1415
);
1516
}
1617
expect.assertions(1);
@@ -21,4 +22,16 @@ describe('createCanal', () => {
2122
const testRenderer = TestRenderer.create(<Canal />);
2223
expect(testRenderer.toJSON()).toMatchSnapshot();
2324
});
25+
26+
it('throws an error if any first arg is not a Component', () => {
27+
try {
28+
// @ts-ignore
29+
createCanal(View, 'aaa');
30+
} catch (error) {
31+
expect(error.message).toBe(
32+
'`createCanal` expects its first arguments to be a React component. Received type for argument 2: string'
33+
);
34+
}
35+
expect.assertions(1);
36+
});
2437
});

src/createCanal.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React, { ComponentType, FunctionComponent } from 'react';
22

3-
export const createCanal = (Page: ComponentType): FunctionComponent => {
4-
if (!(React.isValidElement(Page) || typeof Page === 'function')) {
5-
throw new Error(
6-
`\`createCanal\` expects its first argument to be a React component. Received type: ${typeof Page}`
7-
);
3+
export const createCanal = (...Pages: ComponentType[]): FunctionComponent => {
4+
for (let index = 0; index < Pages.length; index++) {
5+
const Page = Pages[index];
6+
if (!(React.isValidElement(Page) || typeof Page === 'function')) {
7+
throw new Error(
8+
`\`createCanal\` expects its first arguments to be a React component. Received type for argument ${index +
9+
1}: ${typeof Page}`
10+
);
11+
}
812
}
9-
return () => <Page />;
13+
const FirstPage = Pages[0];
14+
return () => <FirstPage />;
1015
};

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { createCanal } from './createCanal';
2-
export { NavigationProvider } from './NavigationProvider';

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"ordered-imports": false,
88
"trailing-comma": false,
99
"no-empty-interface": false,
10-
"member-access": false
10+
"member-access": false,
11+
"arrow-parens": false
1112
},
1213
"rulesDirectory": []
1314
}

0 commit comments

Comments
 (0)