mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-05-03 14:44:40 +08:00
Compare commits
7 Commits
@react-nav
...
@react-nav
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06b3e6bda7 | ||
|
|
3c840bbae3 | ||
|
|
d4ad9d48f9 | ||
|
|
a0e9784d98 | ||
|
|
eff0c0464f | ||
|
|
fe9ba2bf71 | ||
|
|
b0a3756d18 |
@@ -3,6 +3,35 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.7](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.5...@react-navigation/core@5.0.0-alpha.7) (2019-08-31)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* fix navigation object changing too often ([3c840bb](https://github.com/react-navigation/navigation-ex/commit/3c840bb))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add useRoute ([#89](https://github.com/react-navigation/navigation-ex/issues/89)) ([b0a3756](https://github.com/react-navigation/navigation-ex/commit/b0a3756))
|
||||||
|
* support function in screenOptions ([eff0c04](https://github.com/react-navigation/navigation-ex/commit/eff0c04))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.6](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.5...@react-navigation/core@5.0.0-alpha.6) (2019-08-31)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add useRoute ([#89](https://github.com/react-navigation/navigation-ex/issues/89)) ([b0a3756](https://github.com/react-navigation/navigation-ex/commit/b0a3756))
|
||||||
|
* support function in screenOptions ([eff0c04](https://github.com/react-navigation/navigation-ex/commit/eff0c04))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.4...@react-navigation/core@5.0.0-alpha.5) (2019-08-30)
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/core@5.0.0-alpha.4...@react-navigation/core@5.0.0-alpha.5) (2019-08-30)
|
||||||
|
|
||||||
**Note:** Version bump only for package @react-navigation/core
|
**Note:** Version bump only for package @react-navigation/core
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
"react-native",
|
"react-native",
|
||||||
"react-navigation"
|
"react-navigation"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.5",
|
"version": "5.0.0-alpha.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -1,11 +1,22 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { NavigationProp, ParamListBase } from './types';
|
import {
|
||||||
|
NavigationProp,
|
||||||
|
ParamListBase,
|
||||||
|
PartialState,
|
||||||
|
Route,
|
||||||
|
NavigationState,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context which holds the navigation prop for a screen.
|
* Context which holds the navigation prop for a screen.
|
||||||
*/
|
*/
|
||||||
const NavigationContext = React.createContext<
|
const NavigationContext = React.createContext<{
|
||||||
NavigationProp<ParamListBase, string, any, any> | undefined
|
navigation: NavigationProp<ParamListBase, string, any, any> | undefined;
|
||||||
>(undefined);
|
route:
|
||||||
|
| Route<string> & {
|
||||||
|
state?: NavigationState | PartialState<NavigationState>;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
}>({ navigation: undefined, route: undefined });
|
||||||
|
|
||||||
export default NavigationContext;
|
export default NavigationContext;
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ export default function SceneView<
|
|||||||
),
|
),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[getState, route, setState]
|
[getState, route.key, setState]
|
||||||
);
|
);
|
||||||
|
|
||||||
const context = React.useMemo(
|
const context = React.useMemo(
|
||||||
@@ -76,8 +76,16 @@ export default function SceneView<
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const navigationContext = React.useMemo(
|
||||||
|
() => ({
|
||||||
|
navigation,
|
||||||
|
route,
|
||||||
|
}),
|
||||||
|
[navigation, route]
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NavigationContext.Provider value={navigation}>
|
<NavigationContext.Provider value={navigationContext}>
|
||||||
<NavigationStateContext.Provider value={context}>
|
<NavigationStateContext.Provider value={context}>
|
||||||
<EnsureSingleNavigator>
|
<EnsureSingleNavigator>
|
||||||
<StaticContainer
|
<StaticContainer
|
||||||
|
|||||||
@@ -46,11 +46,320 @@ it('sets options with options prop as an object', () => {
|
|||||||
</NavigationContainer>
|
</NavigationContainer>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
expect(root).toMatchInlineSnapshot(`
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
Hello world
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets options with options prop as a fuction', () => {
|
||||||
|
const TestNavigator = (props: any) => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
|
NavigationState,
|
||||||
|
any,
|
||||||
|
{ title?: string },
|
||||||
|
any
|
||||||
|
>(MockRouter, props);
|
||||||
|
const { render, options } = descriptors[state.routes[state.index].key];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>{options.title}</h1>
|
||||||
|
<div>{render()}</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const TestScreen = (): any => 'Test screen';
|
||||||
|
|
||||||
|
const root = render(
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator>
|
||||||
|
<Screen
|
||||||
|
name="foo"
|
||||||
|
component={TestScreen}
|
||||||
|
options={({ route }: any) => ({ title: route.params.author })}
|
||||||
|
initialParams={{ author: 'Jane' }}
|
||||||
|
/>
|
||||||
|
<Screen name="bar" component={jest.fn()} />
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(root).toMatchInlineSnapshot(`
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
Jane
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets options with screenOptions prop as an object', () => {
|
||||||
|
const TestNavigator = (props: any) => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
|
NavigationState,
|
||||||
|
any,
|
||||||
|
{ title?: string },
|
||||||
|
any
|
||||||
|
>(MockRouter, props);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{state.routes.map(route => {
|
||||||
|
const { render, options } = descriptors[route.key];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main key={route.key}>
|
||||||
|
<h1>{options.title}</h1>
|
||||||
|
<div>{render()}</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const TestScreenA = (): any => 'Test screen A';
|
||||||
|
|
||||||
|
const TestScreenB = (): any => 'Test screen B';
|
||||||
|
|
||||||
|
const root = render(
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator screenOptions={{ title: 'Hello world' }}>
|
||||||
|
<Screen name="foo" component={TestScreenA} />
|
||||||
|
<Screen name="bar" component={TestScreenB} />
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(root).toMatchInlineSnapshot(`
|
||||||
|
Array [
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
Hello world
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen A
|
||||||
|
</div>
|
||||||
|
</main>,
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
Hello world
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen B
|
||||||
|
</div>
|
||||||
|
</main>,
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets options with screenOptions prop as a fuction', () => {
|
||||||
|
const TestNavigator = (props: any) => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
|
NavigationState,
|
||||||
|
any,
|
||||||
|
{ title?: string },
|
||||||
|
any
|
||||||
|
>(MockRouter, props);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{state.routes.map(route => {
|
||||||
|
const { render, options } = descriptors[route.key];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main key={route.key}>
|
||||||
|
<h1>{options.title}</h1>
|
||||||
|
<div>{render()}</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const TestScreenA = (): any => 'Test screen A';
|
||||||
|
|
||||||
|
const TestScreenB = (): any => 'Test screen B';
|
||||||
|
|
||||||
|
const root = render(
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator
|
||||||
|
screenOptions={({ route }: any) => ({
|
||||||
|
title: `${route.name}: ${route.params.author || route.params.fruit}`,
|
||||||
|
})}
|
||||||
|
>
|
||||||
|
<Screen
|
||||||
|
name="foo"
|
||||||
|
component={TestScreenA}
|
||||||
|
initialParams={{ author: 'Jane' }}
|
||||||
|
/>
|
||||||
|
<Screen
|
||||||
|
name="bar"
|
||||||
|
component={TestScreenB}
|
||||||
|
initialParams={{ fruit: 'Apple' }}
|
||||||
|
/>
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(root).toMatchInlineSnapshot(`
|
||||||
|
Array [
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
foo: Jane
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen A
|
||||||
|
</div>
|
||||||
|
</main>,
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
bar: Apple
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen B
|
||||||
|
</div>
|
||||||
|
</main>,
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets initial options with setOptions', () => {
|
||||||
|
const TestNavigator = (props: any) => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
|
NavigationState,
|
||||||
|
any,
|
||||||
|
{
|
||||||
|
title?: string;
|
||||||
|
color?: string;
|
||||||
|
},
|
||||||
|
any
|
||||||
|
>(MockRouter, props);
|
||||||
|
const { render, options } = descriptors[state.routes[state.index].key];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1 color={options.color}>{options.title}</h1>
|
||||||
|
<div>{render()}</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const TestScreen = ({ navigation }: any): any => {
|
||||||
|
navigation.setOptions({
|
||||||
|
title: 'Hello world',
|
||||||
|
});
|
||||||
|
|
||||||
|
return 'Test screen';
|
||||||
|
};
|
||||||
|
|
||||||
|
const root = render(
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator>
|
||||||
|
<Screen name="foo" options={{ color: 'blue' }}>
|
||||||
|
{props => <TestScreen {...props} />}
|
||||||
|
</Screen>
|
||||||
|
<Screen name="bar" component={jest.fn()} />
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(root).toMatchInlineSnapshot(`
|
||||||
|
<main>
|
||||||
|
<h1
|
||||||
|
color="blue"
|
||||||
|
>
|
||||||
|
Hello world
|
||||||
|
</h1>
|
||||||
|
<div>
|
||||||
|
Test screen
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates options with setOptions', () => {
|
||||||
|
const TestNavigator = (props: any) => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder<
|
||||||
|
NavigationState,
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
any
|
||||||
|
>(MockRouter, props);
|
||||||
|
const { render, options } = descriptors[state.routes[state.index].key];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1 color={options.color}>{options.title}</h1>
|
||||||
|
<p>{options.description}</p>
|
||||||
|
<caption>{options.author}</caption>
|
||||||
|
<div>{render()}</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const TestScreen = ({ navigation }: any): any => {
|
||||||
|
navigation.setOptions({
|
||||||
|
title: 'Hello world',
|
||||||
|
description: 'Something here',
|
||||||
|
});
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const timer = setTimeout(() =>
|
||||||
|
navigation.setOptions({
|
||||||
|
title: 'Hello again',
|
||||||
|
author: 'Jane',
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
});
|
||||||
|
|
||||||
|
return 'Test screen';
|
||||||
|
};
|
||||||
|
|
||||||
|
const element = (
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator>
|
||||||
|
<Screen name="foo" options={{ color: 'blue' }}>
|
||||||
|
{props => <TestScreen {...props} />}
|
||||||
|
</Screen>
|
||||||
|
<Screen name="bar" component={jest.fn()} />
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
|
||||||
|
const root = render(element);
|
||||||
|
|
||||||
|
act(() => jest.runAllTimers());
|
||||||
|
|
||||||
|
root.update(element);
|
||||||
|
|
||||||
expect(root).toMatchInlineSnapshot(`
|
expect(root).toMatchInlineSnapshot(`
|
||||||
<main>
|
<main>
|
||||||
<h1>
|
<h1
|
||||||
Hello world
|
color="blue"
|
||||||
|
>
|
||||||
|
Hello again
|
||||||
</h1>
|
</h1>
|
||||||
|
<p>
|
||||||
|
Something here
|
||||||
|
</p>
|
||||||
|
<caption>
|
||||||
|
Jane
|
||||||
|
</caption>
|
||||||
<div>
|
<div>
|
||||||
Test screen
|
Test screen
|
||||||
</div>
|
</div>
|
||||||
@@ -295,180 +604,3 @@ it('returns true for canGoBack when parent router handles GO_BACK', () => {
|
|||||||
|
|
||||||
expect(result).toBe(false);
|
expect(result).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets options with options prop as a fuction', () => {
|
|
||||||
const TestNavigator = (props: any) => {
|
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
|
||||||
NavigationState,
|
|
||||||
any,
|
|
||||||
{ title?: string },
|
|
||||||
any
|
|
||||||
>(MockRouter, props);
|
|
||||||
const { render, options } = descriptors[state.routes[state.index].key];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main>
|
|
||||||
<h1>{options.title}</h1>
|
|
||||||
<div>{render()}</div>
|
|
||||||
</main>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const TestScreen = (): any => 'Test screen';
|
|
||||||
|
|
||||||
const root = render(
|
|
||||||
<NavigationContainer>
|
|
||||||
<TestNavigator>
|
|
||||||
<Screen
|
|
||||||
name="foo"
|
|
||||||
component={TestScreen}
|
|
||||||
options={({ route }: any) => ({ title: route.params.author })}
|
|
||||||
initialParams={{ author: 'Jane' }}
|
|
||||||
/>
|
|
||||||
<Screen name="bar" component={jest.fn()} />
|
|
||||||
</TestNavigator>
|
|
||||||
</NavigationContainer>
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(root).toMatchInlineSnapshot(`
|
|
||||||
<main>
|
|
||||||
<h1>
|
|
||||||
Jane
|
|
||||||
</h1>
|
|
||||||
<div>
|
|
||||||
Test screen
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
`);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets initial options with setOptions', () => {
|
|
||||||
const TestNavigator = (props: any) => {
|
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
|
||||||
NavigationState,
|
|
||||||
any,
|
|
||||||
{
|
|
||||||
title?: string;
|
|
||||||
color?: string;
|
|
||||||
},
|
|
||||||
any
|
|
||||||
>(MockRouter, props);
|
|
||||||
const { render, options } = descriptors[state.routes[state.index].key];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main>
|
|
||||||
<h1 color={options.color}>{options.title}</h1>
|
|
||||||
<div>{render()}</div>
|
|
||||||
</main>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const TestScreen = ({ navigation }: any): any => {
|
|
||||||
navigation.setOptions({
|
|
||||||
title: 'Hello world',
|
|
||||||
});
|
|
||||||
|
|
||||||
return 'Test screen';
|
|
||||||
};
|
|
||||||
|
|
||||||
const root = render(
|
|
||||||
<NavigationContainer>
|
|
||||||
<TestNavigator>
|
|
||||||
<Screen name="foo" options={{ color: 'blue' }}>
|
|
||||||
{props => <TestScreen {...props} />}
|
|
||||||
</Screen>
|
|
||||||
<Screen name="bar" component={jest.fn()} />
|
|
||||||
</TestNavigator>
|
|
||||||
</NavigationContainer>
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(root).toMatchInlineSnapshot(`
|
|
||||||
<main>
|
|
||||||
<h1
|
|
||||||
color="blue"
|
|
||||||
>
|
|
||||||
Hello world
|
|
||||||
</h1>
|
|
||||||
<div>
|
|
||||||
Test screen
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
`);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('updates options with setOptions', () => {
|
|
||||||
const TestNavigator = (props: any) => {
|
|
||||||
const { state, descriptors } = useNavigationBuilder<
|
|
||||||
NavigationState,
|
|
||||||
any,
|
|
||||||
any,
|
|
||||||
any
|
|
||||||
>(MockRouter, props);
|
|
||||||
const { render, options } = descriptors[state.routes[state.index].key];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main>
|
|
||||||
<h1 color={options.color}>{options.title}</h1>
|
|
||||||
<p>{options.description}</p>
|
|
||||||
<caption>{options.author}</caption>
|
|
||||||
<div>{render()}</div>
|
|
||||||
</main>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const TestScreen = ({ navigation }: any): any => {
|
|
||||||
navigation.setOptions({
|
|
||||||
title: 'Hello world',
|
|
||||||
description: 'Something here',
|
|
||||||
});
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
const timer = setTimeout(() =>
|
|
||||||
navigation.setOptions({
|
|
||||||
title: 'Hello again',
|
|
||||||
author: 'Jane',
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return () => clearTimeout(timer);
|
|
||||||
});
|
|
||||||
|
|
||||||
return 'Test screen';
|
|
||||||
};
|
|
||||||
|
|
||||||
const element = (
|
|
||||||
<NavigationContainer>
|
|
||||||
<TestNavigator>
|
|
||||||
<Screen name="foo" options={{ color: 'blue' }}>
|
|
||||||
{props => <TestScreen {...props} />}
|
|
||||||
</Screen>
|
|
||||||
<Screen name="bar" component={jest.fn()} />
|
|
||||||
</TestNavigator>
|
|
||||||
</NavigationContainer>
|
|
||||||
);
|
|
||||||
|
|
||||||
const root = render(element);
|
|
||||||
|
|
||||||
act(() => jest.runAllTimers());
|
|
||||||
|
|
||||||
root.update(element);
|
|
||||||
|
|
||||||
expect(root).toMatchInlineSnapshot(`
|
|
||||||
<main>
|
|
||||||
<h1
|
|
||||||
color="blue"
|
|
||||||
>
|
|
||||||
Hello again
|
|
||||||
</h1>
|
|
||||||
<p>
|
|
||||||
Something here
|
|
||||||
</p>
|
|
||||||
<caption>
|
|
||||||
Jane
|
|
||||||
</caption>
|
|
||||||
<div>
|
|
||||||
Test screen
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
`);
|
|
||||||
});
|
|
||||||
|
|||||||
34
packages/core/src/__tests__/useRoute.test.tsx
Normal file
34
packages/core/src/__tests__/useRoute.test.tsx
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import { render } from 'react-native-testing-library';
|
||||||
|
import useNavigationBuilder from '../useNavigationBuilder';
|
||||||
|
import useRoute from '../useRoute';
|
||||||
|
import NavigationContainer from '../NavigationContainer';
|
||||||
|
import Screen from '../Screen';
|
||||||
|
import MockRouter from './__fixtures__/MockRouter';
|
||||||
|
import { RouteProp } from '../types';
|
||||||
|
|
||||||
|
it('gets route prop from context', () => {
|
||||||
|
expect.assertions(1);
|
||||||
|
|
||||||
|
const TestNavigator = (props: any): any => {
|
||||||
|
const { state, descriptors } = useNavigationBuilder(MockRouter, props);
|
||||||
|
|
||||||
|
return state.routes.map(route => descriptors[route.key].render());
|
||||||
|
};
|
||||||
|
|
||||||
|
const Test = () => {
|
||||||
|
const route = useRoute<RouteProp<{ sample: { x: string } }, 'sample'>>();
|
||||||
|
|
||||||
|
expect(route && route.params && route.params.x).toEqual(1);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
render(
|
||||||
|
<NavigationContainer>
|
||||||
|
<TestNavigator>
|
||||||
|
<Screen name="foo" component={Test} initialParams={{ x: 1 }} />
|
||||||
|
</TestNavigator>
|
||||||
|
</NavigationContainer>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -10,6 +10,7 @@ export { default as NavigationContext } from './NavigationContext';
|
|||||||
|
|
||||||
export { default as useNavigationBuilder } from './useNavigationBuilder';
|
export { default as useNavigationBuilder } from './useNavigationBuilder';
|
||||||
export { default as useNavigation } from './useNavigation';
|
export { default as useNavigation } from './useNavigation';
|
||||||
|
export { default as useRoute } from './useRoute';
|
||||||
export { default as useFocusEffect } from './useFocusEffect';
|
export { default as useFocusEffect } from './useFocusEffect';
|
||||||
export { default as useIsFocused } from './useIsFocused';
|
export { default as useIsFocused } from './useIsFocused';
|
||||||
|
|
||||||
|
|||||||
@@ -96,7 +96,12 @@ export type DefaultNavigatorOptions<
|
|||||||
/**
|
/**
|
||||||
* Default options for all screens under this navigator.
|
* Default options for all screens under this navigator.
|
||||||
*/
|
*/
|
||||||
screenOptions?: ScreenOptions;
|
screenOptions?:
|
||||||
|
| ScreenOptions
|
||||||
|
| ((props: {
|
||||||
|
route: RouteProp<ParamListBase, string>;
|
||||||
|
navigation: any;
|
||||||
|
}) => ScreenOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RouterFactory<
|
export type RouterFactory<
|
||||||
@@ -534,11 +539,24 @@ export type TypedNavigator<
|
|||||||
* Navigator component which manages the child screens.
|
* Navigator component which manages the child screens.
|
||||||
*/
|
*/
|
||||||
Navigator: React.ComponentType<
|
Navigator: React.ComponentType<
|
||||||
React.ComponentProps<Navigator> & {
|
Omit<
|
||||||
|
React.ComponentProps<Navigator>,
|
||||||
|
'initialRouteName' | 'screenOptions'
|
||||||
|
> & {
|
||||||
/**
|
/**
|
||||||
* Route to focus on initial render.
|
* Name of the route to focus by on initial render.
|
||||||
|
* If not specified, usually the first route is used.
|
||||||
*/
|
*/
|
||||||
initialRouteName?: keyof ParamList;
|
initialRouteName?: keyof ParamList;
|
||||||
|
/**
|
||||||
|
* Default options for all screens under this navigator.
|
||||||
|
*/
|
||||||
|
screenOptions?:
|
||||||
|
| ScreenOptions
|
||||||
|
| ((props: {
|
||||||
|
route: RouteProp<ParamList, keyof ParamList>;
|
||||||
|
navigation: any;
|
||||||
|
}) => ScreenOptions);
|
||||||
}
|
}
|
||||||
>;
|
>;
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
NavigationState,
|
NavigationState,
|
||||||
ParamListBase,
|
ParamListBase,
|
||||||
RouteConfig,
|
RouteConfig,
|
||||||
|
RouteProp,
|
||||||
Router,
|
Router,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
@@ -20,7 +21,12 @@ type Options<ScreenOptions extends object> = {
|
|||||||
state: NavigationState;
|
state: NavigationState;
|
||||||
screens: { [key: string]: RouteConfig<ParamListBase, string, ScreenOptions> };
|
screens: { [key: string]: RouteConfig<ParamListBase, string, ScreenOptions> };
|
||||||
navigation: NavigationHelpers<ParamListBase>;
|
navigation: NavigationHelpers<ParamListBase>;
|
||||||
screenOptions?: ScreenOptions;
|
screenOptions?:
|
||||||
|
| ScreenOptions
|
||||||
|
| ((props: {
|
||||||
|
route: RouteProp<ParamListBase, string>;
|
||||||
|
navigation: any;
|
||||||
|
}) => ScreenOptions);
|
||||||
onAction: (
|
onAction: (
|
||||||
action: NavigationAction,
|
action: NavigationAction,
|
||||||
visitedNavigators?: Set<string>
|
visitedNavigators?: Set<string>
|
||||||
@@ -93,13 +99,15 @@ export default function useDescriptors<
|
|||||||
return state.routes.reduce(
|
return state.routes.reduce(
|
||||||
(acc, route) => {
|
(acc, route) => {
|
||||||
const screen = screens[route.name];
|
const screen = screens[route.name];
|
||||||
|
const navigation = navigations[route.key];
|
||||||
|
|
||||||
acc[route.key] = {
|
acc[route.key] = {
|
||||||
|
navigation,
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<NavigationBuilderContext.Provider key={route.key} value={context}>
|
<NavigationBuilderContext.Provider key={route.key} value={context}>
|
||||||
<SceneView
|
<SceneView
|
||||||
navigation={navigations[route.key]}
|
navigation={navigation}
|
||||||
route={route}
|
route={route}
|
||||||
screen={screen}
|
screen={screen}
|
||||||
getState={getState}
|
getState={getState}
|
||||||
@@ -108,22 +116,30 @@ export default function useDescriptors<
|
|||||||
</NavigationBuilderContext.Provider>
|
</NavigationBuilderContext.Provider>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
options: {
|
get options() {
|
||||||
// The default `screenOptions` passed to the navigator
|
return {
|
||||||
...screenOptions,
|
// The default `screenOptions` passed to the navigator
|
||||||
// The `options` prop passed to `Screen` elements
|
...(typeof screenOptions === 'object' || screenOptions == null
|
||||||
...(typeof screen.options === 'object' || screen.options == null
|
? screenOptions
|
||||||
? screen.options
|
: screenOptions({
|
||||||
: screen.options({
|
// @ts-ignore
|
||||||
// @ts-ignore
|
route,
|
||||||
route,
|
navigation,
|
||||||
navigation: navigations[route.key],
|
})),
|
||||||
})),
|
// The `options` prop passed to `Screen` elements
|
||||||
// The options set via `navigation.setOptions`
|
...(typeof screen.options === 'object' || screen.options == null
|
||||||
...options[route.key],
|
? screen.options
|
||||||
|
: screen.options({
|
||||||
|
// @ts-ignore
|
||||||
|
route,
|
||||||
|
navigation,
|
||||||
|
})),
|
||||||
|
// The options set via `navigation.setOptions`
|
||||||
|
...options[route.key],
|
||||||
|
};
|
||||||
},
|
},
|
||||||
navigation: navigations[route.key],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
},
|
},
|
||||||
{} as {
|
{} as {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type Options = {
|
|||||||
* Hook to take care of emitting `focus` and `blur` events.
|
* Hook to take care of emitting `focus` and `blur` events.
|
||||||
*/
|
*/
|
||||||
export default function useFocusEvents({ state, emitter }: Options) {
|
export default function useFocusEvents({ state, emitter }: Options) {
|
||||||
const navigation = React.useContext(NavigationContext);
|
const { navigation } = React.useContext(NavigationContext);
|
||||||
const lastFocusedKeyRef = React.useRef<string | undefined>();
|
const lastFocusedKeyRef = React.useRef<string | undefined>();
|
||||||
|
|
||||||
const currentFocusedKey = state.routes[state.index].key;
|
const currentFocusedKey = state.routes[state.index].key;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { NavigationProp, ParamListBase } from './types';
|
|||||||
export default function useNavigation<
|
export default function useNavigation<
|
||||||
T extends NavigationProp<ParamListBase>
|
T extends NavigationProp<ParamListBase>
|
||||||
>(): T {
|
>(): T {
|
||||||
const navigation = React.useContext(NavigationContext);
|
const { navigation } = React.useContext(NavigationContext);
|
||||||
|
|
||||||
if (navigation === undefined) {
|
if (navigation === undefined) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export default function useNavigationCache<
|
|||||||
// Cache object which holds navigation objects for each screen
|
// Cache object which holds navigation objects for each screen
|
||||||
// We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change
|
// We use `React.useMemo` instead of `React.useRef` coz we want to invalidate it when deps change
|
||||||
// In reality, these deps will rarely change, if ever
|
// In reality, these deps will rarely change, if ever
|
||||||
const parentNavigation = React.useContext(NavigationContext);
|
const { navigation: parentNavigation } = React.useContext(NavigationContext);
|
||||||
|
|
||||||
const cache = React.useMemo(
|
const cache = React.useMemo(
|
||||||
() => ({ current: {} as NavigationCache<State, ScreenOptions> }),
|
() => ({ current: {} as NavigationCache<State, ScreenOptions> }),
|
||||||
|
|||||||
@@ -37,7 +37,9 @@ export default function useNavigationHelpers<
|
|||||||
Action extends NavigationAction,
|
Action extends NavigationAction,
|
||||||
EventMap extends { [key: string]: any }
|
EventMap extends { [key: string]: any }
|
||||||
>({ onAction, getState, setState, emitter, router }: Options<State, Action>) {
|
>({ onAction, getState, setState, emitter, router }: Options<State, Action>) {
|
||||||
const parentNavigationHelpers = React.useContext(NavigationContext);
|
const { navigation: parentNavigationHelpers } = React.useContext(
|
||||||
|
NavigationContext
|
||||||
|
);
|
||||||
const { performTransaction } = React.useContext(NavigationStateContext);
|
const { performTransaction } = React.useContext(NavigationStateContext);
|
||||||
|
|
||||||
return React.useMemo(() => {
|
return React.useMemo(() => {
|
||||||
|
|||||||
22
packages/core/src/useRoute.tsx
Normal file
22
packages/core/src/useRoute.tsx
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
import NavigationContext from './NavigationContext';
|
||||||
|
import { ParamListBase, RouteProp } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook to access the route prop of the parent screen anywhere.
|
||||||
|
*
|
||||||
|
* @returns Route prop of the parent screen.
|
||||||
|
*/
|
||||||
|
export default function useRoute<
|
||||||
|
T extends RouteProp<ParamListBase, string>
|
||||||
|
>(): T {
|
||||||
|
const { route } = React.useContext(NavigationContext);
|
||||||
|
|
||||||
|
if (route === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
"We couldn't find a route object. Is your component inside a navigator?"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return route as T;
|
||||||
|
}
|
||||||
@@ -3,6 +3,22 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.6](https://github.com/satya164/navigation-ex/compare/@react-navigation/example@5.0.0-alpha.4...@react-navigation/example@5.0.0-alpha.6) (2019-08-31)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/example
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.5](https://github.com/satya164/navigation-ex/compare/@react-navigation/example@5.0.0-alpha.4...@react-navigation/example@5.0.0-alpha.5) (2019-08-31)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/example
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.4](https://github.com/satya164/navigation-ex/compare/@react-navigation/example@5.0.0-alpha.3...@react-navigation/example@5.0.0-alpha.4) (2019-08-31)
|
# [5.0.0-alpha.4](https://github.com/satya164/navigation-ex/compare/@react-navigation/example@5.0.0-alpha.3...@react-navigation/example@5.0.0-alpha.4) (2019-08-31)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@react-navigation/example",
|
"name": "@react-navigation/example",
|
||||||
"description": "Demo app to showcase various functionality of React Navigation",
|
"description": "Demo app to showcase various functionality of React Navigation",
|
||||||
"version": "5.0.0-alpha.4",
|
"version": "5.0.0-alpha.6",
|
||||||
"private": true,
|
"private": true,
|
||||||
"workspaces": {
|
"workspaces": {
|
||||||
"nohoist": [
|
"nohoist": [
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ import {
|
|||||||
getStateFromPath,
|
getStateFromPath,
|
||||||
NavigationContainerRef,
|
NavigationContainerRef,
|
||||||
} from '@react-navigation/core';
|
} from '@react-navigation/core';
|
||||||
import { useLinking, NativeContainer } from '@react-navigation/native';
|
import {
|
||||||
|
useLinking,
|
||||||
|
NavigationNativeContainer,
|
||||||
|
} from '@react-navigation/native';
|
||||||
import {
|
import {
|
||||||
createDrawerNavigator,
|
createDrawerNavigator,
|
||||||
DrawerNavigationProp,
|
DrawerNavigationProp,
|
||||||
@@ -118,7 +121,7 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NativeContainer
|
<NavigationNativeContainer
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
initialState={initialState}
|
initialState={initialState}
|
||||||
onStateChange={state =>
|
onStateChange={state =>
|
||||||
@@ -177,6 +180,6 @@ export default function App() {
|
|||||||
)}
|
)}
|
||||||
</Drawer.Screen>
|
</Drawer.Screen>
|
||||||
</Drawer.Navigator>
|
</Drawer.Navigator>
|
||||||
</NativeContainer>
|
</NavigationNativeContainer>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,22 @@
|
|||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||||
|
|
||||||
|
# [5.0.0-alpha.7](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.5...@react-navigation/native@5.0.0-alpha.7) (2019-08-31)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/native
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# [5.0.0-alpha.6](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.5...@react-navigation/native@5.0.0-alpha.6) (2019-08-31)
|
||||||
|
|
||||||
|
**Note:** Version bump only for package @react-navigation/native
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.4...@react-navigation/native@5.0.0-alpha.5) (2019-08-29)
|
# [5.0.0-alpha.5](https://github.com/react-navigation/navigation-ex/compare/@react-navigation/native@5.0.0-alpha.4...@react-navigation/native@5.0.0-alpha.5) (2019-08-29)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
"ios",
|
"ios",
|
||||||
"android"
|
"android"
|
||||||
],
|
],
|
||||||
"version": "5.0.0-alpha.5",
|
"version": "5.0.0-alpha.7",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ import useBackButton from './useBackButton';
|
|||||||
* @param props.children Child elements to render the content.
|
* @param props.children Child elements to render the content.
|
||||||
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
* @param props.ref Ref object which refers to the navigation object containing helper methods.
|
||||||
*/
|
*/
|
||||||
|
const NavigationNativeContainer = React.forwardRef(function NativeContainer(
|
||||||
const NativeContainer = React.forwardRef(function NativeContainer(
|
|
||||||
props: NavigationContainerProps,
|
props: NavigationContainerProps,
|
||||||
ref: React.Ref<NavigationContainerRef>
|
ref: React.Ref<NavigationContainerRef>
|
||||||
) {
|
) {
|
||||||
const refContainer = React.useRef<NavigationContainerRef>(null);
|
const refContainer = React.useRef<NavigationContainerRef>(null);
|
||||||
|
|
||||||
useBackButton(refContainer);
|
useBackButton(refContainer);
|
||||||
|
|
||||||
React.useImperativeHandle(ref, () => refContainer.current);
|
React.useImperativeHandle(ref, () => refContainer.current);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -35,4 +35,4 @@ const NativeContainer = React.forwardRef(function NativeContainer(
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default NativeContainer;
|
export default NavigationNativeContainer;
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
export { default as NativeContainer } from './NativeContainer';
|
export {
|
||||||
|
default as NavigationNativeContainer,
|
||||||
|
} from './NavigationNativeContainer';
|
||||||
|
|
||||||
export { default as useBackButton } from './useBackButton';
|
export { default as useBackButton } from './useBackButton';
|
||||||
export { default as useLinking } from './useLinking';
|
export { default as useLinking } from './useLinking';
|
||||||
export { default as useScrollToTop } from './useScrollToTop';
|
export { default as useScrollToTop } from './useScrollToTop';
|
||||||
|
|||||||
Reference in New Issue
Block a user