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. Prometheus入门到放弃(3)之Grafana展示监控数据

    grafana我们这里采用docker方式部署 1.下载镜像 镜像官网地址:https://hub.docker.com/r/grafana/grafana/tags [root@prometheus ...

  2. RDP Error: The Identity Of The Remote Computer Cannot Be Verified

    As i always need to remote to 20 servers at the same time, so i use a tool called Remote Desktop Con ...

  3. php策略模式实现简单计算器

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. quartz2.3.0(十四)trigger触发器优先级排序

    job任务类: package org.quartz.examples.example14; import org.slf4j.Logger; import org.slf4j.LoggerFacto ...

  5. [NOI2008]志愿者招募 (费用流)

    大意: $n$天, 第$i$天要$a_i$个志愿者. $m$种志愿者, 每种无限多, 第$i$种工作时间$[s_i,t_i]$花费$c_i$, 求最少花费. 源点$S$连第一天, 容量$INF$ 第$ ...

  6. 环境配置--升级Python 3.6爬坑

    升级到3.6之后,发现ctrl alt t呼不出命令台,找了半天发现update manager也打不开,而且没有错误报告.....查阅了一番资料看到有人有类似的问题(https://askubunt ...

  7. C盘清理、C盘瘦身、省出30G

    三招C盘瘦身30G,清理win10系统中虚占C盘空间的三大祸害 1.对C盘进行“磁盘清理” C盘右键->属性->磁盘清理->清理系统文件->勾选“windows更新清理”-&g ...

  8. “SQL Server does not exist or access denied.”

    Have resolved the problem, the Port was different and so the Connection String now reads: <connec ...

  9. SQL注入绕过技巧

    1.绕过空格(注释符/* */,%a0): 两个空格代替一个空格,用Tab代替空格,%a0=空格: % % %0a %0b %0c %0d %a0 %00 /**/ /*!*/ 最基本的绕过方法,用注 ...

  10. 关于UIImageView缓存加载的笔记

    加载图片的两个方法: [UIImage imageNamed:] [[UIImage alloc] initWithContentsOfFile: imgpath] [UIImage imageNam ...