【发布时间】:2020-08-07 19:17:12
【问题描述】:
我太难了。 这是代码;
const Destination = (props) => {
const classes = useStyles();
const destinationUrl = props.match.params.dest_url;
const [destination, setDestination] = React.useState();
useEffect(() => {
if (!destination) {
getDestinationByUrl(destinationUrl).then((destination) => {
console.log("destination", destination); //result is attached below
setDestination(destination);
});
}
}, [destination]);
return (
<div className={classes.root}>
<Grid container spacing={3} className={classes.mainGrid}>
{destination && (
<>
<Grid item xs={12}>
<Typography variant="h5" className="title">
{destination.title}
</Typography>
</Grid>
<DestinationRightBar destination={destination} />
</>
)}
</Grid>
</div>
);
};
它在“setDestination(destination);”上给出了这个错误
它还说 DestinationRightBar 发生错误
如果我删除目标右栏,它会起作用! 什么是让它工作的正确方法!
getDestinationByUrl 是一个获取
export function getDestinationByUrl(url) {
return fetch(baseUrl + url, {
method: "GET",
headers: {
Accept: "application/json",
"content-type": "application/json",
Authorization: `Bearer ${TOKEN}`,
},
})
.then((response) =>
response.ok ? response.json().then(handleResponse) : handleNotOk(response)
)
.catch(handleError);
}
DestinationRightBar 只是另一个使用目标对象作为输入的组件。
export default function DestinationRightBar(props) {
const { destination } = props;
【问题讨论】:
-
可以加
getDestinationByUrl代码吗?因为它真的很奇怪 -
我加了,只是抓取
-
当您使用
setDestination设置时,destination的值是多少。 -
destination的值为json对象。
-
我已经编辑了添加的对象
标签: reactjs react-hooks infinite-loop use-effect use-state