Thursday, October 22, 2020

Menggunakan React-Quill di NextJS

 Pertama-tama kita create next app dl:


Setelah itu install react quill

npm install react-quill

Setelah itu edit index.js di root folder next js dan isikan kode ini:


import Head from 'next/head'
import styles from '../styles/Home.module.css'

// import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.snow.css'
import {useState, useEffect, useRef} from 'react'

export default function Home() {
  const [text, setText] = useState('')
  const QuillRef = useRef()
  const [editorLoaded, setEditorLoaded] = useState(false)
  const {ReactQuill} = QuillRef.current || {}

  const modules = {
    toolbar: [
      [{ 'header': [1, 2, false] }],
      ['bold', 'italic', 'underline','strike', 'blockquote'],
      [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}],
      ['link', 'image'],
      ['clean']
    ],
  }

  const formats = [
    'header',
    'bold', 'italic', 'underline', 'strike', 'blockquote',
    'list', 'bullet', 'indent',
    'link', 'image'
  ]

  useEffect(() => {
    QuillRef.current = {
      ReactQuill: require('react-quill')
    }
    setEditorLoaded(true)
  }, [])


  const handleChange = (val) => {
    setText(val)
    console.log(val)
  }
  return (
    <div className={styles.container}>
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <h2> Testing React Quill in Next JS </h2>
      {editorLoaded &&
        <ReactQuill value={text}
        onChange={(val) => handleChange(val)}
        modules={modules}
        formats={formats}
        />
      }
      
      
    </div>
  )
}

Daan hasilnya :


Klu kita ngetik dan masukin image :



No comments:

Post a Comment