Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, which returns a random quote and a loading state.

Remember, hooks can only be used inside function components.

function useStarWarsQuote() {
// defualt the quote to a empty value
const [quote, setQuote] = useState('')
// default the loading to false
const [loading, setLoading] = useState(false) useEffect(() => {
async function getStarWarsQuote() {
setLoading(true)
// 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
setQuote(quote)
setLoading(false)
}
getStarWarsQuote()
}, []) return { quote, loading }
}
export function FeedbackCustomComponent() {
const [text, setText] = useText('')
const { quote, loading } = useStarWarsQuote() useEffect(() => {
if (quote) {
setText(quote)
}
}, [quote, setText])
// Handle form submission
function handleSubmit(e) {
e.preventDefault()
console.log(`Submitting response to API: "${text}"`)
setText('')
} // Update text in state onchange for textarea
function handleTextChange(e) {
const updatedText = e.target.value setText(updatedText)
} if (loading) return <p>Loading...</p> if (quote) {
return (
<Form onSubmit={e => handleSubmit(e)}>
<Title>Custom Example</Title>
<Label>
Have feedback for our team? <br /> Let us know here

[React] Write a Custom React Effect Hook的更多相关文章

  1. [NPM + React] Prepare a Custom React Hook to be Published as an npm Package

    Before we publish our package, we want to make sure everything is set up correctly. We’ll cover vers ...

  2. [React] Use the React Effect Hook in Function Components

    Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...

  3. 【React 资料备份】React Hook

    Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...

  4. [React] Reduce Code Redundancy with Custom React Hooks

    In this lesson, we'll cover how to create a custom React hook for managing the state of any input. T ...

  5. React报错之React Hook 'useEffect' is called in function

    正文从这开始~ 总览 为了解决错误"React Hook 'useEffect' is called in function that is neither a React function ...

  6. React报错之React hook 'useState' cannot be called in a class component

    正文从这开始~ 总览 当我们尝试在类组件中使用useState 钩子时,会产生"React hook 'useState' cannot be called in a class compo ...

  7. [React] Validate Custom React Component Props with PropTypes

    In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...

  8. React报错之React Hook useEffect has a missing dependency

    正文从这开始~ 总览 当useEffect钩子使用了一个我们没有包含在其依赖数组中的变量或函数时,会产生"React Hook useEffect has a missing depende ...

  9. React报错之React hook 'useState' is called conditionally

    正文从这开始~ 总览 当我们有条件地使用useState钩子时,或者在一个可能有返回值的条件之后,会产生"React hook 'useState' is called conditiona ...

随机推荐

  1. #redis 安装命令

    redis 安装命令 cd /usr/local sudo wget http://download.redis.io/releases/redis-4.0.11.tar.gz sudo tar xf ...

  2. SpringAOP异常:org.springframework.aop.aspectj.AspectJExpressionPointcut cannot be cast to com.....

    Exception in thread "main" java.lang.ClassCastException: org.springframework.aop.aspectj.A ...

  3. css页面加载动画

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. SpringCloud之Ribbon负载均衡及Feign消费者调用服务

    目的: 微服务调用Ribbon Ribbon负载均衡 Feign简介及应用 微服务调用Ribbon Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.U ...

  5. pdfplumber模块初始用

    import pdfplumber import re def pdf_read(): pdf=pdfplumber.open('文件路径'")#文件路径,读取文件 page0=pdf.pa ...

  6. Java Service Wrapper将jar包安装成Windows服务

    刚接触java,第一次使用Java开发windows服务,也是刚不久看了SSM框架 简直也是一头雾水,不过只要用心理解,其实很简单,下面有详细的步骤,包学包会 在windows上运行jar包,需要在工 ...

  7. Oracle数据库中导出某张表到SQL并关联更新

    首先想到查询出结果,然后导出为SQL文件: 先导出表结构 1 在桌面建立对应的sql空文件 2 toos-->export userObjects 3 在对话框中选择你要导出的表 4 勾选上si ...

  8. 1)NET CORE 重新认识 .net & .net core

    最近想系统性的学习下.net core ,在这之前我想再重新的认识下.net ,以及跟.net core 的区别. 有些我们开发.net经常用到的词汇可能还不是很了解,或者不能清晰的出他们的关系与却别 ...

  9. 并发编程之Java锁

    一.重入锁 锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchronized(重量级) 和 ReentrantLock(轻量级)等等 ) .这些已经写好提供的锁为我们开发提 ...

  10. Flutter 与 Android 的交互

    https://juejin.im/post/5cd91de4518825686b120921 https://juejin.im/entry/5b64292be51d451995676398