zoukankan      html  css  js  c++  java
  • [React] Write a Custom State Hook in React

    Writing your own custom State Hook is not as a daunting as you think. To keep things simple, we'll refactor our text state value that uses useState and instead create a custom hook called useText.

    function useText(initialValue) {
      return useState(initialValue)
    }
    export function FeedbackCustomComponent() {
      const [text, setText] = useText('')
      useEffect(() => {
        async function getStarWarsQuote() {
          // Get initial text
          const response = await fetch(
            'https://starwars-quote-proxy-gi0d3x1lz.now.sh/api/randomQuote'
          )
          const data = await response.json()
          const quote = data.starWarsQuote
          setText(quote)
        }
        getStarWarsQuote()
      }, [setText])
  • 相关阅读:
    面试
    二叉树- 二叉树直径
    排序算法
    JAVA编程
    JAVA编程
    JAVA中break和continue的区别
    HTTP的序列化和反序列化
    PL/SQL基础
    G. Game Design
    hdu 6703 array
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11773753.html
Copyright © 2011-2022 走看看