简体   繁体   English

重定向(反应路由器dom),给出了期望的赋值或函数调用,而是看到了一个表达式no-unused-expressions

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

I'm new Reactjs, I'm using fetch for api call. 我是新的Reactjs,正在使用fetch进行api调用。 I want to redirect the page with api data on some other page, for that I'm using below code 我想使用其他页面上的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 }
                 }}/>
            });


    }

I'm using Redirect from react-router-dom but getting below error after calling api 我正在使用从react-router-dom Redirect ,但在调用api后得到以下错误

Expected an assignment or function call and instead saw an expression no-unused-expressions 期望一个赋值或函数调用,而是看到一个表达式no-unused-expressions

Search for the keywords to learn more about each error. 搜索关键字以了解有关每个错误的更多信息。

Here they have mentioned that this is an eslint error and not an error from the router. 在这里,他们提到这是错误错误,而不是路由器的错误。

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

To solve your problem you need to to use redirect with return. 为了解决您的问题,您需要使用带有return的重定向。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 得到预期的赋值或函数调用的错误,而是在反应中看到了一个表达式 no-unused-expressions - getting an error of Expected an assignment or function call and instead saw an expression no-unused-expressions in react 期望一个赋值或函数调用,而是在React中看到一个表达式no-unused-expressions错误 - Expected an assignment or function call and instead saw an expression no-unused-expressions error in React 在React中导入文件时,获得期望的赋值或函数调用,而是看到一个表达式no-unused-expressions - Getting Expected an assignment or function call and instead saw an expression no-unused-expressions, when import a file in React 反应:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions - React: expected an assignment or function call and instead saw an expression no-unused-expressions 期望一个赋值或 function 调用,而是在尝试使用 react 制作组件时看到一个表达式 no-unused-expressions - Expected an assignment or function call and instead saw an expression no-unused-expressions when trying to make a component using react 获得预期的赋值或函数调用,而是在尝试在 React 中呈现组件时看到表达式 no-unused-expressions 错误 - Getting Expected an assignment or function call and instead saw an expression no-unused-expressions error when trying to render a component in React 得到预期赋值或函数调用的错误,而是看到一个表达式 no-unused-expressions? - Getting the error that Expected an assignment or function call and instead saw an expression no-unused-expressions? Object.keys-需要一个赋值或函数调用,而是看到一个表达式no-unused-expressions - Object.keys - Expected an assignment or function call and instead saw an expression no-unused-expressions 如何解决这个 期望赋值或函数调用,而是看到一个表达式 no-unused-expressions - how to solve this Expected an assignment or function call and instead saw an expression no-unused-expressions “预期分配或 function 调用,而是在我的聊天应用程序中看到一个表达式 no-unused-expressions” - “Expected an assignment or function call and instead saw an expression no-unused-expressions” in my chat app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM