簡體   English   中英

重定向(反應路由器dom),給出了期望的賦值或函數調用,而是看到了一個表達式no-unused-expressions

[英]Redirect (react router dom) giving Expected an assignment or function call and instead saw an expression no-unused-expressions

我是新的Reactjs,正在使用fetch進行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-dom Redirect ,但在調用api后得到以下錯誤

期望一個賦值或函數調用,而是看到一個表達式no-unused-expressions

搜索關鍵字以了解有關每個錯誤的更多信息。

在這里,他們提到這是錯誤錯誤,而不是路由器的錯誤。

參考: https : //github.com/ReactTraining/react-router/issues/4718

為了解決您的問題,您需要使用帶有return的重定向。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM