0

I'm new to react and started working with basic routes, i have a serious doubt in routing.

  1. I Have a 3 pages "Welcome.js" , "Signin.js, "Signup.js".

  2. "Welcome.js" contains signin and signup button.

  3. clicking on signin and signup button, it goes to its respective pages.

  4. both the signin and signup pages contains a backbutton, onclicking on backbutton it goes to welcome.js page.

  5. signin page contains text "if your a newuser go to signup" and signup page contains text "already a user, go to login". on clicking on that texts, it goes to corresponding pages.

so my problem is, if i click the "if your a newuser go to signup" and enters the signup page and pressing backbutton it goes to "welcome page". i want it to go to the signin page because the signup page comes from clicking the text from signin page.

kindly give me idea, so i can implement this routing in my future works.

2 Answers 2

1

I will give you a basic example on how I use routing, perhaps this will help. First I have a src/routes folder that contains all of my routes. Inside this folder we have an src/routes/index.js which handles all of the routes.

class Routes extends Component {
    render() {
        return (
            <BrowserRouter>
                <Switch>
                    <Route exact path="/" render={props => <Dashboard {...props} />} />
                    <Route path="/login" render={props => <Login {...props} />} />
                    <Route path="/register" render={props => <Register {...props} />} />
                </Switch>
            </BrowserRouter>
            )
        }
    }
export default Routes;

Then import all routes in src/App.js

import React, { Component } from 'react'
import Routes from './routes'; 

class App extends Component {
  render() {
    return (
      <div>
        <Routes />
      </div>
    )
  }
}

export default App;

Lastly in src/index.js to the dom

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';


ReactDOM.render(<App />, document.getElementById('root'));

If you want to use a link in any one your pages simply

import { Link } from 'react-router-dom';

<Link to="/login">
  <Button type="button">
     Login
  </Button>
</Link>

react-router-dom even has a redirect....

import { Redirect } from 'react-router-dom';

var { redirect } = this.state;
   if(redirect) 
      return <Redirect to='/login' />

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Tim, thanks for your solution. i will check it tomorrow and update you. thanks.
1

This should work for you. You can use React router and the withRouter hoc to get access to the history prop to push to other screens.

import { Router, Route, Switch, withRouter } from 'react-router';

const welcome = withRouter(({ history }) => {
  return (
    <>
      <button onClick={() => history.push('/signup')}>Signup</button>
      <button onClick={() => history.push('/sigin')}>SignIn</button>
    </>
  );
})

const signIn = withRouter(({ history }) => {
  return (
    <button onClick={() => history.push('/signup')} />
  );
})

const signUp = withRouter(({ history }) => {
  return (
    <button onClick={() => history.push('/signin')} />
  );
})

const App = () => {
  return (<Router history={history}>
    <Switch>
      <Route path="/welocome" component={welcome} />
      <Route path="/signin" component={signin} />
      <Route path="/signup" component={signup} />
    </Switch>
  </Router>);
}

1 Comment

Thanks Johnny peter, i will try this and say, in the meantime, can you spare a moment to share me a codepen or codesandbox link to the small demo of it? your help means a lot for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.