[英]My React Js website is running perfectly on my pc but when I tried to run it on my phone it shows, Cannot read properties of undefined (Reading 'map')
import React, { Component } from "react";
import Nitems from "./Nitems";
import Loading from "./Loading";
import './Font.css'
import PropTypes from 'prop-types'
export class News extends Component {
static defaultProps={
country: "in",
category: "general"
}
static propTypes={
country: PropTypes.string,
category: PropTypes.string
}
constructor() {
super();
console.log("Hii this is a constructor");
this.state = {
articles: [],
loading: false,
page: 1,
};
}
async componentDidMount() {
this.props.setProgress(10)
let url =
`https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&pagesize=12`;
this.setState({loading: true})
let data = await fetch(url);
this.props.setProgress(30)
let parsedata = await data.json();
this.props.setProgress(70)
this.setState({
articles: parsedata.articles,
totalarticles: parsedata.totalResults,
loading: false
});
this.props.setProgress(100)
}
previouspage = async () => {
this.props.setProgress(10)
let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}$apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
this.state.page - 1
}&pagesize=12`;
this.setState({loading: true})
let data = await fetch(url);
let parsedata = await data.json();
console.log(parsedata);
this.setState({
page: this.state.page - 1,
articles: parsedata.articles,
loading: false
});
this.props.setProgress(100)
};
nextpage = async () => {
this.props.setProgress(10)
if (this.state.page + 1 > Math.ceil(this.state.totalarticles / 12)) {
}
else {
let url = `https://newsapi.org/v2/top-headlines?country=${this.props.country}&category=${this.props.category}&apiKey=c7908fc25bbc49589fcaedcedfb2e114&page=${
this.state.page + 1
}&pagesize=12`;
this.setState({loading: true})
let data = await fetch(url);
let parsedata = await data.json();
console.log(parsedata);
this.setState({
page: this.state.page + 1,
articles: parsedata.articles,
loading: false
});
this.props.setProgress(100)
}
};
render() {
return (
<div className="container ">
<h3 className="my-3" className="text-center" style={{margin: "18px 0px", fontFamily:" Permanent Marker", fontSize: "45px"}}>Top Headlines 💥</h3>
{this.state.loading && <Loading/>}
<div className="row">
{!this.state.loading && this.state.articles.map((element) => {
return (
<div className="col-md-4" key={element.url}>
<Nitems
title={element.title.slice(0, 50)}
description={
element.description
? element.description.slice(0, 45)
: "Description is not available. The ADP"
}
img={element.urlToImage}
nurl={element.url}
date={element.publishedAt}
author={element.source.name}
></Nitems>
</div>
);
})}
</div>
<div class="d-flex justify-content-between">
<button
disabled={this.state.page <= 1}
type="button"
class="btn btn-danger"
onClick={this.previouspage}
>
👈 Previous
</button>
<button disabled={(this.state.page + 1 > Math.ceil(this.state.totalarticles / 12))} type="button" class="btn btn-danger" onClick={this.nextpage}>
Next 👉
</button>
</div>
</div>
);
}
}
export default News;
My React Js website is running perfectly on my pc but when I tried to run it on my phone it shows, Cannot read properties of undefined (Reading 'map').我的 React Js 网站在我的电脑上运行完美,但当我尝试在我的手机上运行它时,它显示无法读取未定义的属性(读取“地图”)。
If i got the problem, then this is causing the trouble;如果我遇到了问题,那么这就是问题所在; Inside your
render
method;在你的
render
方法中;
{!this.state.loading && this.state.articles.map((element)
As you are fetching articles from some url, it takes some time so, first, this.state.articles
is undefined.当您从一些 url 获取文章时,需要一些时间,因此,首先,
this.state.articles
是未定义的。
So try this;所以试试这个;
{!this.state.loading && this.state.articles && this.state.articles.map((element)
Try to add the ?
尝试添加
?
after articles and then try it.看完文章再试试吧。 after adding this you will not get the
cannot read property of undefined
error.添加此后,您将不会获得
cannot read property of undefined
。 And you will able to debug you app.你将能够调试你的应用程序。
{!this.state.loading && this.state.articles?.map((element) => {
return (
<div className="col-md-4" key={element.url}>
<Nitems
title={element.title.slice(0, 50)}
description={
element.description
? element.description.slice(0, 45)
: "Description is not available. The ADP"
}
img={element.urlToImage}
nurl={element.url}
date={element.publishedAt}
author={element.source.name}
></Nitems>
</div>
);
})}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.