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

Commit ff73720

Browse files
committed
Control closing state via a prop
1 parent d33521a commit ff73720

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

src/App.tsx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ type CustomRoute = Route & { initial?: boolean };
99

1010
type State = {
1111
routes: CustomRoute[];
12+
closing: string[];
1213
};
1314

1415
export default class App extends React.Component<{}, State> {
15-
state = {
16+
state: State = {
1617
routes: [{ key: '0', initial: true }, { key: '1', initial: true }],
18+
closing: [],
1719
};
1820

1921
private key = 2;
@@ -26,35 +28,42 @@ export default class App extends React.Component<{}, State> {
2628
<Card
2729
{...rest}
2830
key={route.key}
31+
closing={this.state.closing.includes(route.key)}
2932
onClose={() =>
3033
this.setState(state => ({
3134
routes: state.routes.filter(r => r !== route),
35+
closing: state.closing.filter(key => key !== route.key),
3236
}))
3337
}
3438
gesturesEnabled={index !== 0}
3539
animationsEnabled={!route.initial}
3640
{...SlideFromRightIOS}
3741
>
38-
{({ close }: { close: () => void }) => (
39-
<View style={styles.scene}>
40-
<Text style={styles.item}>{index}</Text>
42+
<View style={styles.scene}>
43+
<Text style={styles.item}>{index}</Text>
44+
<View style={styles.item}>
45+
<Button
46+
title="Add screen"
47+
onPress={() => {
48+
this.setState(state => ({
49+
routes: [...state.routes, { key: String(this.key++) }],
50+
}));
51+
}}
52+
/>
53+
</View>
54+
{index !== 0 ? (
4155
<View style={styles.item}>
4256
<Button
43-
title="Add screen"
44-
onPress={() => {
57+
title="Go back"
58+
onPress={() =>
4559
this.setState(state => ({
46-
routes: [...state.routes, { key: String(this.key++) }],
47-
}));
48-
}}
60+
closing: [...state.closing, route.key],
61+
}))
62+
}
4963
/>
5064
</View>
51-
{index !== 0 ? (
52-
<View style={styles.item}>
53-
<Button title="Go back" onPress={close} />
54-
</View>
55-
) : null}
56-
</View>
57-
)}
65+
) : null}
66+
</View>
5867
</Card>
5968
);
6069
};

src/components/Card.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import memoize from '../utils/memoize';
1616
type Props = {
1717
focused: boolean;
1818
stale: boolean;
19+
closing?: boolean;
1920
next?: Animated.Node<number>;
2021
current: Animated.Value<number>;
2122
layout: { width: number; height: number };
2223
direction: 'horizontal' | 'vertical';
2324
onOpen?: () => void;
2425
onClose?: () => void;
25-
children: (props: { close: () => void }) => React.ReactNode;
26+
children: React.ReactNode;
2627
animationsEnabled: boolean;
2728
gesturesEnabled: boolean;
2829
transitionSpec: {
@@ -79,7 +80,7 @@ export default class Card extends React.Component<Props> {
7980
};
8081

8182
componentDidUpdate(prevProps: Props) {
82-
const { layout, direction, animationsEnabled } = this.props;
83+
const { layout, direction, closing, animationsEnabled } = this.props;
8384
const { width, height } = layout;
8485

8586
if (
@@ -103,6 +104,10 @@ export default class Card extends React.Component<Props> {
103104
direction === 'vertical' ? DIRECTION_VERTICAL : DIRECTION_HORIZONTAL
104105
);
105106
}
107+
108+
if (closing !== prevProps.closing) {
109+
this.isClosing.setValue(closing ? TRUE : FALSE);
110+
}
106111
}
107112

108113
private isVisible = new Value<Binary>(TRUE);
@@ -321,8 +326,6 @@ export default class Card extends React.Component<Props> {
321326
},
322327
]);
323328

324-
private handleClose = () => this.isClosing.setValue(TRUE);
325-
326329
// We need to ensure that this style doesn't change unless absolutely needs to
327330
// Changing it too often will result in huge frame drops due to detaching and attaching
328331
// Changing it during an animations can result in unexpected results
@@ -388,9 +391,7 @@ export default class Card extends React.Component<Props> {
388391
active={stale ? 0 : 1}
389392
style={styles.screen}
390393
>
391-
{children({
392-
close: this.handleClose,
393-
})}
394+
{children}
394395
</Screen>
395396
</Animated.View>
396397
</PanGestureHandler>

0 commit comments

Comments
 (0)