Skip to content

React-Router with React Native, history change anywhere in the code #5229

@ghost

Description

Hello,

I try to use history push anywhere in the code, so

my render method is just:

myhistory.js

class MyHistory {
	history;

	constructor() {
		this.history = createMemoryHistory({
			initialEntries: ['/'], // The initial URLs in the history stack
			initialIndex: 0, // The starting index in the history stack
			keyLength: 10, // The length of location.key
			// A function to use to confirm navigation with the user. Required
			// if you return string prompts from transition hooks (see below)
			getUserConfirmation: null,
		});
	}

}

export default new MyHistory;
// all of my import ..
import { MemoryRouter, Route, Switch } from 'react-router-native';
import MyHistory from './myhistory';
class MyApp extends React.Component {
	render() {
		return (
			<MemoryRouter history={MyHistory.history}>
                                <Switch>
					<Route exact path="/" component={MainScene} />
					<Route path="/user" component={UserScene} />
					<Route path="/test" component={TestScene} />
                                </Switch>
			</MemoryRouter>
		);
	}
}
AppRegistry.registerComponent('MyApp', () => MyApp);

In other parts of my code I tried to call like this:

import MyHistory from './myhistory';
MyHistory.history.push('/user');

I don't know if it is a bug in react native or me

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions