重定向(React Router dom)给出了预期的分配或功能调用,而是看到了一个不使用表达的表达式



我是新的reactjs,我正在使用fetch for for api调用。我想在其他页面上使用API数据重定向,因为我正在使用以下代码

import React, { Component } from 'react';
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';
import { Link,Redirect  } from 'react-router-dom';
fetchCatList(){
        let catID = "5c2f74e8a4d846591b2b1a41";
        // fetch("http://sfsdfsdfsdf/api/listing",{
        // method: 'POST',
        // body: JSON.stringify({
     //        category_id: catID,       
     //      }),
        // headers:{
        //  'Content-Type':'application/json',
        // },
        // })
     //      .then(res => res.json())
     //      .then(json => this.setState({catlist:json.data}));  
          fetch("http://sdfsdfsdf/api/listing", {
              method: "post",
              headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
              },
              //make sure to serialize your JSON body
              body: JSON.stringify({
                category_id: catID,             
              })
            })
            .then(function(res){ return res.json(); })
            .then(function(response){ 
               //do something awesome that makes the world a better place              
               console.log(response);
               <Redirect to={{
                    pathname: '/listing_page',
                    state: { listData: response }
                 }}/>
            });

    }

我正在使用react-router-domRedirect,但是在调用API

后,出现了以下错误

期望分配或功能调用,而是看到了一个表达式 没有悬挂表达

搜索关键字以了解有关每个错误的更多信息。

要解决您的问题,您需要使用返回重定向。

render () {
  if(this.state.toListing) {
    return <Redirect push to="/listing_page" />
  }
  return (
    ...
  )
}

在这里他们提到这是一个eslint错误,而不是路由器的错误。

参考:https://github.com/reacttraining/react-router/issues/4718

相关内容

最新更新