ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • #2-7 컴포넌트 분리하기
    React로 nodebirdSNS 만들기 (인프런)/#2 SNS 화면 만들기 2020. 1. 7. 18:16

    - 반복문 조건문 form 등이 쓰인 코드들을 컴포넌트로 많이 분리함

     

    - index.js

    import React from "react";
    import PostForm from "../components/PostForm";
    import PostCard from "../components/PostCard";
    
    // index.js는 타임라인 역할
    
    const dummy = {
      isLoggedIn: true,
      imagePaths: [],
      mainPosts: [
        {
          User: {
            id: 1,
            nickname: "정중식"
          },
          content: "첫 번째 게시글",
          img: "https://img.hankyung.com/photo/201807/03.17342954.1.jpg"
        }
      ]
    };
    
    const Home = () => {
      return (
        <div>
          {dummy.isLoggedIn && <PostForm />}
          {dummy.mainPosts.map(c => {
            return <PostCard key={c} post={c} />;
          })}
        </div>
      );
    };
    
    export default Home;
    

     

    - profile.js

    import React from "react";
    import { Button, List, Icon, Card } from "antd";
    import NicknameEditForm from "../components/NicknameEditForm";
    
    // 내가 팔로잉 하고있는 사람 혹은 내가 쓴 게시글 볼 수있는 화면
    
    const Profile = () => {
      return (
        <div>
          <NicknameEditForm />
          <List
            style={{ marginBottom: "20px" }}
            grid={{ gutter: 4, xs: 2, md: 3 }}
            size="small"
            header={<div>팔로잉 목록</div>}
            loadMore={<Button style={{ width: "100%" }}>더 보기</Button>}
            bordered
            dataSource={["정중식", "정백구", "정백설"]}
            renderItem={item => (
              <List.Item style={{ marginTop: "20px" }}>
                <Card actions={[<Icon key="stop" type="stop" />]}>
                  <Card.Meta description={item} />
                </Card>
              </List.Item>
            )}
          />
          <List
            style={{ marginBottom: "20px" }}
            grid={{ gutter: 4, xs: 2, md: 3 }}
            size="small"
            header={<div>팔로워 목록</div>}
            loadMore={<Button style={{ width: "100%" }}>더 보기</Button>}
            bordered
            dataSource={["정중식", "정백구", "정백설"]}
            renderItem={item => (
              <List.Item style={{ marginTop: "20px" }}>
                <Card actions={[<Icon key="stop" type="stop" />]}>
                  <Card.Meta description={item} />
                </Card>
              </List.Item>
            )}
          />
        </div>
      );
    };
    
    export default Profile;
    

     

    - PostCard.js

    import React from "react";
    import { Button, Card, Avatar, Icon } from "antd";
    import PropTypes from "prop-types";
    
    const PostCard = ({ post }) => {
      return (
        <Card
          key={+post.createdAt}
          cover={post.img && <img alt="example" src={post.img} />}
          actions={[
            <Icon type="retweet" key="retweet" />,
            <Icon type="heart" key="heart" />,
            <Icon type="message" key="message" />,
            <Icon type="ellipsis" key="ellipsis" />
          ]}
          extra={<Button>팔로우</Button>}
        >
          <Card.Meta
            avatar={<Avatar>{post.User.nickname[0]}</Avatar>}
            title={post.User.nickname}
            description={post.content}
          />
        </Card>
      );
    };
    // shape는 객체
    PostCard.propTypes = {
      post: PropTypes.shape({
        User: PropTypes.object,
        content: PropTypes.string,
        img: PropTypes.string,
        createdAt: PropTypes.object
      })
    };
    
    export default PostCard;
    

     

    - PostForm.js

    import React from "react";
    import { Form, Input, Button } from "antd";
    
    const dummy = {
      isLoggedIn: true,
      imagePaths: [],
      mainPosts: [
        {
          User: {
            id: 1,
            nickname: "정중식"
          },
          content: "첫 번째 게시글",
          img: "https://img.hankyung.com/photo/201807/03.17342954.1.jpg"
        }
      ]
    };
    
    const PostForm = () => {
      return (
        <Form style={{ margin: "10px 0 20px" }} encType="multipart/form-data">
          <Input.TextArea
            maxLength={140}
            placeholder="어떤 신기한 일이 있었나요?"
          />
          <div>
            <input type="file" multiple hidden />
            <Button>이미지 업로드</Button>
            <Button type="primary" style={{ float: "right" }} htmlType="submit">
              짹짹
            </Button>
          </div>
          <div>
            {dummy.imagePaths.map((v, i) => {
              return (
                <div key={v} style={{ display: "inline-block" }}>
                  <img
                    src={"http/localhost:3000/" + v}
                    style={{ width: "200px" }}
                    alt={v}
                  />
                  <div>
                    <Button>제거</Button>
                  </div>
                </div>
              );
            })}
          </div>
        </Form>
      );
    };
    
    export default PostForm;
    

     

    - UserProfile.js

    import React from "react";
    import { Card, Avatar } from "antd";
    
    const UserProfile = () => {
      return (
        <Card
          actions={[
            <div key="twit">
              짹짹
              <br />
              {dummy.Post.length}
            </div>,
            <div key="following">
              팔로잉
              <br />
              {dummy.Following.length}
            </div>,
            <div key="follower">
              팔로워
              <br />
              {dummy.Follower.length}
            </div>
          ]}
        >
          <Card.Meta
            avatar={<Avatar>{dummy.nickname[0]}</Avatar>}
            title={dummy.nickname}
          />
        </Card>
      );
    };
    
    export default UserProfile;
    

     

    - NicknameEditForm.js

    import React from "react";
    import { Button, Form, Input } from "antd";
    
    const NicknameEditForm = () => {
      return (
        <Form
          style={{
            marginBottom: "20px",
            border: "1px solid #d9d9d9",
            padding: "20px"
          }}
        >
          <Input addonBefore="닉네임" />
          <Button type="primary">수정</Button>
        </Form>
      );
    };
    
    export default NicknameEditForm;
    
    

     

     

    댓글

Designed by Tistory.