Javascript 重定向(react router dom)给出预期的赋值或函数调用,并看到一个表达式没有未使用的表达式
我是新的Reactjs,我正在使用fetch进行api调用。我想在另一个页面上用api数据重定向页面,为此我使用下面的代码Javascript 重定向(react router dom)给出预期的赋值或函数调用,并看到一个表达式没有未使用的表达式,javascript,reactjs,react-router-dom,Javascript,Reactjs,React Router Dom,我是新的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.defau
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 }
}}/>
});
}
import React,{Component}来自'React';
从“反应猫头鹰转盘”导入猫头鹰转盘;
导入“owl.carousel/dist/assets/owl.carousel.css”;
导入“owl.carousel/dist/assets/owl.theme.default.css”;
从“react router dom”导入{Link,Redirect};
fetchCatList(){
让catID=“5c2f74e8a4d846591b2b1a41”;
//取回(“http://sfsdfsdfsdf/api/listing",{
//方法:“POST”,
//正文:JSON.stringify({
//类别识别号:catID,
// }),
//标题:{
//“内容类型”:“应用程序/json”,
// },
// })
//.then(res=>res.json())
//.then(json=>this.setState({catlist:json.data}));
取回(“http://sdfsdfsdf/api/listing", {
方法:“张贴”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
},
//确保序列化JSON正文
正文:JSON.stringify({
类别识别号:catID,
})
})
.then(函数(res){return res.json();})
.then(函数(响应){
//做一些很棒的事情,让世界变得更美好
控制台日志(响应);
});
}
我正在使用来自react router dom
的Redirect
,但在调用api后出现以下错误
应为赋值或函数调用,而不是看到表达式
没有未使用的表达式
搜索关键字以了解有关每个错误的更多信息
这里他们提到这是一个eslint错误,而不是来自路由器的错误
请参阅:要解决您的问题,您需要使用带返回的重定向
render () {
if(this.state.toListing) {
return <Redirect push to="/listing_page" />
}
return (
...
)
}
render(){
if(本州toListing){
返回
}
返回(
...
)
}