[React] Write a Custom React Effect Hook
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的更多相关文章
- [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 ...
- [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 ...
- 【React 资料备份】React Hook
Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...
- [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 ...
- React报错之React Hook 'useEffect' is called in function
正文从这开始~ 总览 为了解决错误"React Hook 'useEffect' is called in function that is neither a React function ...
- React报错之React hook 'useState' cannot be called in a class component
正文从这开始~ 总览 当我们尝试在类组件中使用useState 钩子时,会产生"React hook 'useState' cannot be called in a class compo ...
- [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 ...
- React报错之React Hook useEffect has a missing dependency
正文从这开始~ 总览 当useEffect钩子使用了一个我们没有包含在其依赖数组中的变量或函数时,会产生"React Hook useEffect has a missing depende ...
- React报错之React hook 'useState' is called conditionally
正文从这开始~ 总览 当我们有条件地使用useState钩子时,或者在一个可能有返回值的条件之后,会产生"React hook 'useState' is called conditiona ...
随机推荐
- as3鱼眼放大镜
package { //hi.baidu.com/inuko //bitmapdata fisheye magnifier //原创代码,如有雷同,纯属巧合 /* 本例是使用近似算法,只是最简单的鱼眼 ...
- python爬虫-80电子书,爬图片
''' 作者:Caric_lee 日期:2018 查看图片 ''' import requests from bs4 import BeautifulSoup r = requests.get(&qu ...
- Dubbo学习摘录(一)
Dubbo基于自定义配置,实现自己的Bean. 通常,完成一个自定义配置需要以下步骤: 设计配置属性和JavaBean: 编写XSD文件: 编写NamespaceHandler和BeanDefinit ...
- new Image 读取宽高为0——onload
获取图片一张图片的大小 let img = new Image() img.src = imgUrl if ( img.width != 375 || img.height != 200 ) { me ...
- CCF 2016-12-1 中间数
CCF 2016-12-1 中间数 题目 问题描述 在一个整数序列a1, a2, -, an中,如果存在某个数,大于它的整数数量等于小于它的整数数量,则称其为中间数.在一个序列中,可能存在多个下标不相 ...
- WPF 不要给 Window 类设置变换矩阵(分析篇):System.InvalidOperationException: 转换不可逆。
原文:WPF 不要给 Window 类设置变换矩阵(分析篇):System.InvalidOperationException: 转换不可逆. 最近总是收到一个异常 "System.Inva ...
- 2.5_Database Interface ODBC数据源及案例
分类 用户数据源 用户创建的数据源,称为“用户数据源”.此时只有创建者才能使用,并且只能在所定义的机器上运行.任何用户都不能使用其他用户创建的用户数据源. 系统数据源 所有用户在Windows下以服务 ...
- Python之TensorFlow的(案例)验证码识别-6
一.这里的案例相对比较简单,主要就是通过学习验证码的识别来认识深度学习中我们一般在工作中,需要处理的东西会存在哪些东西. 二.因为我没有数据集,没有关系,这里自己写了一个数据集,来做测试,为了方便我把 ...
- “SQL Server does not exist or access denied.”
Have resolved the problem, the Port was different and so the Connection String now reads: <connec ...
- ubuntu 迅雷 XwareDesktop
Xinkai/XwareDesktop Ubuntu上编译安装说明 Home Ubuntu上编译安装说明 使用说明 升级到0.12 升级到0.9 发行版支持情况 名 ...