[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 ...
随机推荐
- Response的应用
1.HttpServletResponse概述 service方法中的response的类型是ServletResponse,而doGet/doPost方法的response的类型是HttpServl ...
- PMBOK(第六版) PMP备考知识总汇!
记录本人学习PMBOK第六版的学习笔记. 备考知识总汇! PMBOK序章 PMP备考指南之相关事项介绍 PMP备考指南之第一章:引论 PMP备考指南之第二章:项目运作环境 PMP备考指南之第三章:项目 ...
- Python规范:用用assert
什么是assert assert的语法: assert_stmt ::= "assert" expression ["," expression] 例: ass ...
- github上热门深度学习项目
github上热门深度学习项目 项目名 Stars 描述 TensorFlow 29622 使用数据流图进行可扩展机器学习的计算. Caffe 11799 Caffe:深度学习的快速开放框架. [Ne ...
- [LOJ2292] [THUSC2016] 成绩单
题目链接 LOJ:https://loj.ac/problem/2292 洛谷:https://www.luogu.org/problemnew/show/P5336 Solution 区间\(\rm ...
- Lucene BooleanQuery中的Occur.MUST与Occur.Should
https://www.cnblogs.com/weipeng/archive/2012/04/18/2455079.html 1. 多个MUST的组合不必多说,就是交集 2. MUST和SH ...
- C#破解dll
使用反编译工具对dll文件进行反编译,找到校验过期的相关代码,反编译工具可以使用ILSpy或Reflector; 使用ildasm.exe工具将dll导出成il文本文件,在该文件中找到相关的代码进行修 ...
- python 简单工厂模式
abc 是抽象类模块abc.ABC 是继承抽象类 也可直接继承 (metaclass=ABCMeta)abc.abstractmethod 是定义抽象方法 简单工厂模式:通过接口创建对象,但不会暴露 ...
- 用于RISC-V的Makefile示例
# Initialize ASM For RISC-V .section .text.entry .option norvc .global _start .macro push_reg addi s ...
- CRM, C4C和SAP Hybris的数据库层设计
SAP的product都是DB provider无关的. CRM大家都很熟悉了,application developer最多用Open SQL直接操作表. Netweaver里支持的DB provi ...