Git Product home page Git Product logo

blog-proto's Introduction

Blog 만들기

  • Firebase 사용

  • react-toastify

  • react-hook-form

###react-toastify

  • App에서 ToastContainer를 넣어주면 어느 컴포넌트에서든 toast() 를 사용해 토스트메세지를 날릴 수 있음
  import React from 'react';

  import { ToastContainer, toast } from 'react-toastify';
  import 'react-toastify/dist/ReactToastify.css';
  
  function App(){
    const notify = () => toast("Wow so easy!");

    return (
      <div>
        <button onClick={notify}>Notify!</button>
        <ToastContainer />
      </div>
    );
  }
  • 컴포넌트에 여러 타입을 지정해서 커스텀 할 수 있음
    <ToastContainer
    position="top-right" 
    autoClose={5000}
    hideProgressBar={false}
    newestOnTop={false}
    closeOnClick
    rtl={false}
    pauseOnFocusLoss
    draggable
    pauseOnHover
    />
    <ToastContainer />

    toast.success('🦄 Wow so easy!', {  // toast 뒤에 메서드를 붙여서 여러종류의 타입 사용 가능
        position: "top-right",
        autoClose: 5000,
        hideProgressBar: false,
        closeOnClick: true,
        pauseOnHover: true,
        draggable: true,
        progress: undefined,
    });


###react-hook-form

  • 리액트에서 form을 쉽게 다룰 수 있는 라이브러리
import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
    const { register, handleSubmit, watch, formState: { errors } } = useForm();
    const onSubmit = data => console.log(data);


    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <input defaultValue="test" {...register("id")} />

            <input {...register("password", { required: true })} />
            
            {errors.exampleRequired && <span>This field is required</span>}

            <input type="submit" />
        </form>
    );
}
  • register() 안쪽에 form이 submit 될 때 해당 input의 value를 넣어줄 key값과 validation(옵션)을 넣어줌.
  • 위의 예시대로 하면 form의 handleSubmit()가 리턴해주는 값은 { id :'xxxx' , password :'yyyy' } 형식의 객체가 될 것.
export default function App() {
    const { register, handleSubmit, watch, formState: { errors } } = useForm();
    const onSubmit = data => console.log(data);

    console.log(errors); // error : {email: {ref: input.sc-cZMNgc.kVScea, type: 'isValidEmail', message: ''}}
    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <input
                type="text"
                {...register('email', {
                    required: true,
                    validate: { [errorTypes.IS_VALID_EMAIL]: (value) => Validate.isValidEmail(value) },
                })}
            />
        </form>
    ); 
  • validate 는 유효성 검사를 할때 사용됨.
  • validate 의 value로 { [errorType] : () => {} } 형식을 사용. 콜백이 false를 리턴하면 error.type으로 [errorType]이 나온다.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.