Skip to content

Commit ecfbb76

Browse files
committed
refactor(example): remove alternative ways of using StackContainer in example (#146)
## Description - **Remove alternative way of providing `StackContainer` with path configuration** It unnecessarily complicates things, we don't need it. In both approaches we need to specify available screens (paths in current nomenclature) up front anyway. It is fine to specify just the config. ## Test code and steps to reproduce `TestScreenStack` ## Checklist - [ ] Ensured that CI passes
1 parent 2a7b27b commit ecfbb76

File tree

2 files changed

+15
-55
lines changed

2 files changed

+15
-55
lines changed

apps/src/tests/TestScreenStack/StackContainer.tsx

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ export const StackContainerPath = (_: Path) => {
3333
};
3434

3535
interface StackContainerProps {
36-
children?: {
37-
props: Path;
38-
}[];
39-
config?: Path[];
36+
pathConfigs: Path[];
4037
options?: StackProps;
4138
}
4239

@@ -64,35 +61,25 @@ export function useStackNavigation() {
6461
return context;
6562
}
6663

67-
export function StackContainer({ children, config }: StackContainerProps) {
68-
const [stack, setStack] = React.useState<Screen[]>([]);
64+
function requireNotEmptyPaths(pathConfigs?: Path[]) {
65+
if (pathConfigs == null || pathConfigs.length === 0) {
66+
throw new Error('[RNScreens] There must be at least one path configured');
67+
}
68+
}
6969

70-
const paths: Path[] = React.useMemo(() => {
71-
const configuredPaths =
72-
config?.length !== undefined && config?.length
73-
? config
74-
: children?.map(child => child.props);
75-
76-
if (
77-
configuredPaths?.length === undefined ||
78-
configuredPaths?.length === 0
79-
) {
80-
throw new Error(
81-
'There must be at least one path configured, please define paths using StackContainerPath components or the config prop',
82-
);
83-
}
70+
export function StackContainer({ pathConfigs }: StackContainerProps) {
71+
requireNotEmptyPaths(pathConfigs);
8472

85-
return configuredPaths;
86-
}, [children, config]);
73+
const [stack, setStack] = React.useState<Screen[]>([]);
8774

8875
const pathsMap = React.useMemo(
89-
() => Object.fromEntries(paths.map(path => [path.name, path])),
90-
[paths],
76+
() => Object.fromEntries(pathConfigs.map(config => [config.name, config])),
77+
[pathConfigs],
9178
);
9279

9380
const push = React.useCallback(
9481
(name: string) => {
95-
const requestedPath = pathsMap?.[name];
82+
const requestedPath = pathsMap[name];
9683

9784
if (!requestedPath) {
9885
throw new Error(`Path with name ${name} is not defined`);
@@ -148,15 +135,15 @@ export function StackContainer({ children, config }: StackContainerProps) {
148135

149136
React.useEffect(() => {
150137
if (stack.length === 0) {
151-
const firstPath = paths?.[0]?.name;
138+
const firstPath = pathConfigs[0].name;
152139

153140
if (!firstPath) {
154141
throw new Error('There must be at least one path defined');
155142
}
156143

157144
push(firstPath);
158145
}
159-
}, [paths, push, stack.length]);
146+
}, [pathConfigs, push, stack.length]);
160147

161148
console.log('StackContainer render', stack);
162149

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
import React from 'react';
22

3-
4-
// Alternative way to define stack
5-
6-
// import { Button } from 'react-native';
7-
// import { ScreenLayout } from './ScreenLayout';
8-
// import { StackContainer, StackContainerPath, useStackNavigation } from './StackContainer';
9-
10-
// const TestComponent = () => {
11-
// const navigation = useStackNavigation();
12-
13-
// return (
14-
// <ScreenLayout>
15-
// <Button onPress={() => navigation.push('A')} title="Push A" />
16-
// <Button onPress={() => navigation.push('B')} title="Push B" />
17-
// </ScreenLayout>
18-
// );
19-
// };
20-
21-
// export default function App() {
22-
// return (
23-
// <StackContainer>
24-
// <StackContainerPath name="A" component={TestComponent} />
25-
// <StackContainerPath name="B" component={TestComponent} />
26-
// </StackContainer>
27-
// );
28-
// }
29-
303
import { generateStackWithNames } from './helper';
314
import { StackContainer } from './StackContainer';
325

336
export default function App() {
347
return (
35-
<StackContainer config={generateStackWithNames(['A', 'B', 'C'])}/>
8+
<StackContainer pathConfigs={generateStackWithNames(['A', 'B', 'C'])} />
369
);
3710
}

0 commit comments

Comments
 (0)