我在 localhost:3000 上使用假 db.json 并在另一台服务器上运行react.js。React.js 代码的新内容。
我需要获取 db.json 文件中评论星级的平均值。
这是破坏网站的代码功能......
const getAverage = (reviews) => {
reviews.map((review) => {
return review.stars.reduce((a.review.stars, b.review.stars => (a.review.stars + b.review.stars), 0) / review.stars.length;
});
};
我调用它的方式总是有语法错误......
<div>
<p className="totalReviewsTally">Average: {getAverage(reviews.stars)} {reviews.length}</p>
</div>
我的组文件
header.js
import React, {useState, useEffect} from "react";
import './index.css';
import './images/gup_logo.svg';
import './images/gup_logo_text.svg';
import './images/mapMarker.svg';
import './images/solidStar.svg';
import './images/emptyStar.svg';
import './images/halfStar.svg';
import './images/makeFaveShop.svg';
import './images/unFaveShop.svg';
import './images/contactButton.svg';
import './images/shopCart.svg';
import { Link } from "@reach/router";
const Header = (props) => {
const [shopOwnerInfo, setShopOwnerInfo] = useState([]);
const [reviews, setReviews] = useState([]);
useEffect(() => {
const getJsonOwnerData = async () => {
const response = await fetch(`http://www.localhost:3000/shopOwner/1`);
const responseShopOwner = await response.json();
setShopOwnerInfo(responseShopOwner);
};
const getReviews = async () => {
const response = await fetch(`http://www.localhost:3000/reviews`);
const reviewsResponse = await response.json();
setReviews(reviewsResponse);
};
getReviews();
getJsonOwnerData();
}, []);
const getAverage = (reviews) => {
reviews.map((review) => {
return review.stars.reduce((a.review.stars, b.review.stars => (a.review.stars + b.review.stars), 0) / review.stars.length;
});
};
动漫人物
猛跑小猪
相关分类