user11783025
user11783025

Reputation:

How to route from one page to another?

I was trying to route the page if my http response is successful to the new page i.e. landing page. I have written code for handling http request and its working fine and returning response, if my response is successfull or the login is successful i.e. true then i want to move to my next page i.e my component <Loading /> with some parameter if my response fails then it should be on the samepage

Basically i was trying when i click on login button it should send a http request if the request return a response then it should switch over to another page else it should be on the same page

const Login = () => {

    const [userName , setUserName] = useState("")
    const [userPassword , setUserPassword] = useState("")


    const handleUsernameInput = (event) => {
        setUserName(event.target.value);
        console.log("username entered")
    }

    const handlePasswordInput = (event) => {
        setUserPassword(event.target.value);
        console.log("password entered")
    }

    const [httpData, apiMethod] = useHttpPOSTHandlerdotthen()

    const handleSubmit = () => {
        apiMethod({url: 'url' , data: { "password": userPassword, "username": userName }})
        setUserName("")
        setUserPassword("")
        nextPage();
    }

    const nextPage = () => {
        if (httpData) {
        return <Redirect to={{ pathname: '/landing', key: httpData.key}} />
        }
        else{
            return <Redirect to={{ pathname: '/' }} /> 
        }
    }

    return (
        <div className = "login-page">
            <Form>
                <Form.Control size = "sm" 
                  type="text" 
                  placeholder="Username" 
                  value = {userName}
                  onChange = {event => handleUsernameInput(event)} 
                  />
                <Form.Control size = "sm" 
                     type="password" 
                     placeholder="Password" 
                     value = {userPassword}
                     onChange = {event => handlePasswordInput(event)} 
                     />
                <Button size = "sm" variant="success" 
                onClick = {handleSubmit}>Login</Button>
            </Form>
        </div>
    );
};

export default Login;

Upvotes: 2

Views: 735

Answers (2)

James
James

Reputation: 82096

You aren't far away, you can render the Redirect component to perform a HTTP redirect from one page to another when using React Router e.g.

if (httpData) return <Redirect to={{ pathname: '/landing', state: { key: httpData.key } }} />;

// render the login page otherwise

Then inside Landing, you can access the key via this.props.location.state.key.

Upvotes: 0

William Scalabre
William Scalabre

Reputation: 653

I think you will find all the infos you need in this other thread: How to push to History in React Router v4?

using history is a simple and efficient way to manage "moving" between pages

you can use history.push() function to achieve what you want

Upvotes: 2

Related Questions