[英]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.