如何在Reactjs的新react-router-dom中使用Redirect

mao*_*cio 94 javascript reactjs react-router

我正在使用最新版本的react-router模块,名为react-router-dom,在使用React开发Web应用程序时已成为默认设置.我想知道如何在POST请求后进行重定向.我一直在制作这段代码,但在请求之后,没有任何反应.我在网上查看,但所有数据都是关于反应路由器的先前版本,而不是最后一次更新.

码:

import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { Redirect } from 'react-router'

import SignUpForm from '../../register/components/SignUpForm';
import styles from './PagesStyles.css';
import axios from 'axios';
import Footer from '../../shared/components/Footer';

class SignUpPage extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      errors: {},
      client: {
        userclient: '',
        clientname: '',
        clientbusinessname: '',
        password: '',
        confirmPassword: ''
      }
    };

    this.processForm = this.processForm.bind(this);
    this.changeClient = this.changeClient.bind(this);
  }

  changeClient(event) {
    const field = event.target.name;
    const client = this.state.client;
    client[field] = event.target.value;

    this.setState({
      client
    });
  }

  async processForm(event) {
    event.preventDefault();

    const userclient = this.state.client.userclient;
    const clientname = this.state.client.clientname;
    const clientbusinessname = this.state.client.clientbusinessname;
    const password = this.state.client.password;
    const confirmPassword = this.state.client.confirmPassword;
    const formData = { userclient, clientname, clientbusinessname, password, confirmPassword };

    axios.post('/signup', formData, { headers: {'Accept': 'application/json'} })
      .then((response) => {
        this.setState({
          errors: {}
        });

        <Redirect to="/"/> // Here, nothings happens
      }).catch((error) => {
        const errors = error.response.data.errors ? error.response.data.errors : {};
        errors.summary = error.response.data.message;

        this.setState({
          errors
        });
      });
  }

  render() {
    return (
      <div className={styles.section}>
        <div className={styles.container}>
          <img src={require('./images/lisa_principal_bg.png')} className={styles.fullImageBackground} />
          <SignUpForm 
            onSubmit={this.processForm}
            onChange={this.changeClient}
            errors={this.state.errors}
            client={this.state.client}
          />
          <Footer />
        </div>
      </div>
    );
  }
}

export default SignUpPage;
Run Code Online (Sandbox Code Playgroud)

Seb*_*ald 152

您必须使用setState设置将呈现方法<Redirect>内部的属性render().

例如

class MyComponent extends React.Component {
  state = {
    redirect: false
  }

  handleSubmit () {
    axios.post(/**/)
      .then(() => this.setState({ redirect: true }));
  }

  render () {
    const { redirect } = this.state;

     if (redirect) {
       return <Redirect to='/somewhere'/>;
     }

     return <RenderYourForm/>;
}
Run Code Online (Sandbox Code Playgroud)

您还可以在官方文档中看到一个示例:https://reacttraining.com/react-router/web/example/auth-workflow


也就是说,我建议你将API调用放在服务或其他东西中.然后你可以使用该history对象以编程方式进行路由.这就是与redux集成的工作原理.

但我想你有理由这样做.

  • 注意必须包含Redirect才能在文件开头使用它:从{react-router-dom'导入{Redirect} (4认同)
  • 是的,引擎盖下的`Redirect`正在调用`history.replace`.如果要访问`history`对象,请使用`withRoutet` /`Route`. (3认同)
  • `react-router` &gt;=5.1 现在包含钩子,所以你可以只 `const History = useHistory(); 历史记录.push("/myRoute")` (2认同)

Tha*_*yen 31

由于useHistory() 钩子, React Router v5 现在允许您使用 history.push() 简单地重定向:

import { useHistory } from "react-router-dom"

function HomeButton() {
  let history = useHistory()

  function handleClick() {
    history.push("/home")
  }

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  )
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*hli 26

这里有一个小例子作为对所有提到的例子的标题的回应在我看来和官方的一样复杂.

您应该知道如何转换es2015以及使您的服务器能够处理重定向.这是快递的片段.有关此内容的更多信息,请点击此处.

请务必将其置于所有其他路线下方.

const app = express();
app.use(express.static('distApp'));

/**
 * Enable routing with React.
 */
app.get('*', (req, res) => {
  res.sendFile(path.resolve('distApp', 'index.html'));
});
Run Code Online (Sandbox Code Playgroud)

这是.jsx文件.注意最长的路径是如何出现的并且更加通用.对于最常规的路由,请使用exact属性.

// Relative imports
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';

// Absolute imports
import YourReactComp from './YourReactComp.jsx';

const root = document.getElementById('root');

const MainPage= () => (
  <div>Main Page</div>
);

const EditPage= () => (
  <div>Edit Page</div>
);

const NoMatch = () => (
  <p>No Match</p>
);

const RoutedApp = () => (
  <BrowserRouter >
    <Switch>
      <Route path="/items/:id" component={EditPage} />
      <Route exact path="/items" component={MainPage} />          
      <Route path="/yourReactComp" component={YourReactComp} />
      <Route exact path="/" render={() => (<Redirect to="/items" />)} />          
      <Route path="*" component={NoMatch} />          
    </Switch>
  </BrowserRouter>
);

ReactDOM.render(<RoutedApp />, root); 
Run Code Online (Sandbox Code Playgroud)

  • 不错的示例... **没有类定义** (2认同)

Kor*_*vis 6

尝试这样的事情。

import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { Redirect } from 'react-router'

import SignUpForm from '../../register/components/SignUpForm';
import styles from './PagesStyles.css';
import axios from 'axios';
import Footer from '../../shared/components/Footer';

class SignUpPage extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      errors: {},
      callbackResponse: null,
      client: {
        userclient: '',
        clientname: '',
        clientbusinessname: '',
        password: '',
        confirmPassword: ''
      }
    };

    this.processForm = this.processForm.bind(this);
    this.changeClient = this.changeClient.bind(this);
  }

  changeClient(event) {
    const field = event.target.name;
    const client = this.state.client;
    client[field] = event.target.value;

    this.setState({
      client
    });
  }

  processForm(event) {
    event.preventDefault();

    const userclient = this.state.client.userclient;
    const clientname = this.state.client.clientname;
    const clientbusinessname = this.state.client.clientbusinessname;
    const password = this.state.client.password;
    const confirmPassword = this.state.client.confirmPassword;
    const formData = { userclient, clientname, clientbusinessname, password, confirmPassword };

    axios.post('/signup', formData, { headers: {'Accept': 'application/json'} })
      .then((response) => {
        this.setState({
          callbackResponse: {response.data},
        });
      }).catch((error) => {
        const errors = error.response.data.errors ? error.response.data.errors : {};
        errors.summary = error.response.data.message;

        this.setState({
          errors
        });
      });
  }

const renderMe = ()=>{
return(
this.state.callbackResponse
?  <SignUpForm 
            onSubmit={this.processForm}
            onChange={this.changeClient}
            errors={this.state.errors}
            client={this.state.client}
          />
: <Redirect to="/"/>
)}

  render() {
    return (
      <div className={styles.section}>
        <div className={styles.container}>
          <img src={require('./images/lisa_principal_bg.png')} className={styles.fullImageBackground} />
         {renderMe()}
          <Footer />
        </div>
      </div>
    );
  }
}

export default SignUpPage;
Run Code Online (Sandbox Code Playgroud)


Vir*_*ngh 6

Update for react-router-dom v6, there is a useNavigate hook for condtional redirection and Link component

import { useEffect } from 'react';
import { useNavigate, Link } from 'react-router-dom';

export default function Example(): JSX.Element {
  const navigate = useNavigate();

  useEffect(() => {
      ...
      if(true) { // conditional redirection
       navigate('/not-found', { replace: true });
      }
  }, []);

  return (
    <>
     ...
     <Link to="/home"> Home </Link> // relative link navigation to /home 
     ...
    </>
  );
}
Run Code Online (Sandbox Code Playgroud)

useNavigate
Relative Link Component


Mas*_*lah 5

只需在您喜欢的任何函数中调用它即可。

this.props.history.push('/main');
Run Code Online (Sandbox Code Playgroud)

  • 外部组件怎么样? (2认同)

Vij*_*mma 5

注意:仅回答问题标题

以前的版本

<Redirect from="/old-url" to="/new-url" />
Run Code Online (Sandbox Code Playgroud)

最新版本

<Route path="/old-url" element={<Navigate to="/new-url" />} />
Run Code Online (Sandbox Code Playgroud)