Javascript 数组在react中以状态存储时成为对象
我正在从api中获取一些数据。在提取数据的回调中,数据是一个数组:Javascript 数组在react中以状态存储时成为对象,javascript,reactjs,Javascript,Reactjs,我正在从api中获取一些数据。在提取数据的回调中,数据是一个数组: const classes = useStyles(); const [posts, setPosts] = useState([]); useEffect(() => { fetch('https://forum-temp.herokuapp.com/api/posts') .then((res) => res.json()) .then((res) => {
const classes = useStyles();
const [posts, setPosts] = useState([]);
useEffect(() => {
fetch('https://forum-temp.herokuapp.com/api/posts')
.then((res) => res.json())
.then((res) => {
console.log(typeof res.data.docs) // Array
setPosts(res.data.docs);
});
}, []);
在我的功能组件的return语句中-
<div className="post__container">
{ {posts.map((post, i) => (
<MiniPost
dp="https://picsum.photos/200"
username="blah"
time="blah"
heading="blah"
comments="4"
history={history}
key={i}
/>
))} }
</div>
{{posts.map((post,i)=>(
))} }
这是因为你是双重包装。React只使用一个包装。只使用一组像这样的{}
{posts.map(…)}:
{posts.map((post,i)=>(
))}
这是因为你是双重包装。React只使用一个包装。只使用一组像这样的{}
{posts.map(…)}:
{posts.map((post,i)=>(
))}
非常感谢。当然,我想马上做,但它要求等几分钟,所以很抱歉delay@Akash再次感谢。祝您有个美好的一天!非常感谢你当然,我想马上做,但它要求等几分钟,所以很抱歉delay@Akash再次感谢。祝您有个美好的一天!