reactjs - 解决:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
问题描述
嘿,我是该领域的新手,想使用地图显示状态,但我无法这样做
,通过 axios 的 api 调用没有问题,所以忽略它我的代码是
import React, { Component } from 'react';
import axios from 'axios';
const url ='https://www.reddit.com/r/space.json';
class Apicall extends Component {
state={
posts:[],
subr:'space'
};
componentDidMount(){
this.getReddit();
}
getReddit=async()=>{
console.log('getredddit called sir ');
try {
let response=await axios.get(`https://www.reddit.com/r/${this.state.subr}.json`);
let posts=response.data.data.children.map(obj=>obj.data)
this.setState({posts:posts},()=>{console.log(this.state.posts);
})
} catch (error) {console.log(error);}}
render() {
let controlItems=this.state.posts.map(post=>{<h1 id={post.id}>{post.title}</h1>});
return (
<div>
<h1>{`/r/${this.state.subr}`} </h1>
{controlItems}
</div>);
}
}
export default Apicall;
解决方案
您在数据上迭代错误。response.data.data.children应替换为response.data.children。et controlItems=this.state.posts.map(post=>{{post.title}})的隐式返回;也是错误的。
import React from "react";
import "./styles.css";
export default function App() {
return (
<div className="App">
<Apicall />
</div>
);
}
class Apicall extends React.Component {
state = {
posts: [],
subr: "space"
};
componentDidMount() {
this.getReddit();
}
getReddit = async () => {
console.log("getredddit called sir ");
try {
let response = await fetch(
`https://www.reddit.com/r/${this.state.subr}.json`
);
response = await response.json();
console.log(response);
let posts = response.data.children.map(obj => obj.data);
this.setState({ posts: posts }, () => {
console.log(this.state.posts);
});
} catch (error) {
console.log(error);
}
};
render() {
let controlItems = this.state.posts.map(post => (
<h1 id={post.id}>{post.title}</h1>
));
return (
<div>
<h1>{`/r/${this.state.subr}`} </h1>
{controlItems}
</div>
);
}
}
推荐阅读
- assembly - 为什么我在 LC-3 模拟器中看到 R3 寄存器而不是 R1,就像我在 sample.bin 文件中所写的那样?
- python - 如何实时更新情节中的点?
- firebase - 如何与 UI 一起更新 Nativescript ObservableArray 中的项目值?
- python - super() 带参数和不带参数有什么区别?
- latex - 使用带有降价的 pandoc 在第四层嵌套太深?
- android - Android单元测试模拟firebase
- css - 为什么 marginBottom 在 img 和 span 标签中的行为不同?(虽然两者都有“显示:内联”?)
- reactjs - 将输入参数连接到拼接调用中
- javascript - 我有一个代码,它将类存储在本地存储中以用于不同的元素,但其中一个没有存储?任何想法为什么会发生这种情况?
- javascript - DataMap/D3 - 如何重置缩放?