微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React- 从 JSON 服务器获取数据并在卡片内显示

如何解决React- 从 JSON 服务器获取数据并在卡片内显示

我正在尝试使用 json 服务器(获取、发布、删除)创建一个简单的博客网站。在主页中,正文组件将显示每个博客文章标题图片文章的几行,点击后将显示完整的文章。它将类似于这张图片

我在 Bootstrap 5 和 4 中使用 Material Design,我从那里找到了这个组件,但是从 posts.map 函数中它不起作用。 这就是我想要实现的-

enter image description here

这是我的代码输出-

enter image description here

这是我的代码

import React,{ useState,useEffect } from 'react';
import {
  MDbrow,MDBCol,MDBCard,MDBCardBody,MDBMask,MDBIcon,MDBView,MDBBtn,} from 'mdbreact';
import axios from 'axios';


const Body = () => {
  const [posts,setPost] = useState([]);

  useEffect(() => {
    loadData();
  },[]);

  const loadData = async () => {
    const result = await axios.get('http://localhost:3001/post');
    setPost(result.data);
  };

  return (
    <MDBCard className='my-5 px-5 pb-5'>
      <MDBCardBody>
        <h2 className='h1-responsive font-weight-bold text-center my-5'>
          Recent posts
        </h2>

> /// map from json server to show the data inside of MDbrow component ///

        {posts.map((post,index) => {
          <MDbrow>
            <MDBCol lg='5' xl='4'>
              <MDBView hover className='rounded z-depth-1-half mb-lg-0 mb-4'>
                <img
                  className='img-fluid'
                  src='https://mdbootstrap.com/img/Photos/Others/images/49.jpg'
                  alt=''
                />
                <a href='#!'>
                  <MDBMask overlay='white-slight' />
                </a>
              </MDBView>
            </MDBCol>
            <MDBCol lg='7' xl='8'>
              <h3 className='font-weight-bold mb-3 p-0'>
                <strong>{post.title}</strong>
              </h3>
              <p className='dark-grey-text'>{post.body}</p>
              <p>
                by{' '}
                <a href='#!' className='font-weight-bold'>
                  Jessica Clark
                </a>,19/04/2018
                <MDBBtn tag='a'>
                  <i
                    class='fas fa-edit'
                    size='lg'
                    style={{ color: 'blue' }}></i>
                </MDBBtn>
                <MDBBtn tag='a'>
                  <i
                    class='fas fa-trash-alt'
                    size='lg'
                    style={{ color: 'red' }}></i>
                </MDBBtn>
              </p>
              <MDBBtn color='primary' size='md'>
                Read More
              </MDBBtn>
              <MDBBtn color='danger' size='sm'>
                Delete
              </MDBBtn>
            </MDBCol>
          </MDbrow>;
        })}
        <hr className='my-5' />
      </MDBCardBody>
    </MDBCard>
  );
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。