How to pass params with history.push/Link/Redirect in react-router v4?

前端 未结 10 1093
你的背包
你的背包 2020-11-22 04:27

How can we pass parameter with this.props.history.push(\'/page\') in React-Router v4?

.then(response => {
       var r = this;
        if (re         


        
10条回答
  •  旧时难觅i
    2020-11-22 04:55

    To use React 16.8+(withHooks) you can use this way

    import React from 'react';
    import { useHistory } from 'react-router-dom';
    
    export default function SomeFunctionalComponent() {
    let history = useHistory(); // should be called inside react component
    
    const handleClickButton = () => {    
    "funcionAPICALL"
           .then(response => {
                 if (response.status >= 200 && response.status < 300) {
                     history.push('/template');
                  });
    }
    
    return ( 
    Some component stuff

    To make API POST request and redirect to "/template" click a button API CALL

    ) }

    Source here to read more https://reacttraining.com/react-router/web/example/auth-workflow

提交回复
热议问题