Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side effects in function components. The Effect Hook is called by passing a function as the first argument. Here, you can perform side effects. If needed, you can pass an optional second argument, which is an array of dependencies. This tells React, "Only run my effect when these values change."

A tip from Ryan Florence on using the dependency array:

"with which state does this effect synchronize with."

import React, { useState, useEffect } from 'react'
import { Form, Label, Textarea, Button, Title } from './Feedback.styles' export function FeedbackEffectComponent() {
const [text, setText] = useState('')
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()
}, []) // 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)
} return (
<Form onSubmit={e => handleSubmit(e)}>
<Title>Effect Example</Title>
<Label>
Have feedback for our team? <br /> Let us know here

[React] Use the React Effect Hook in Function Components的更多相关文章

  1. [React] Write a Custom React Effect Hook

    Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...

  2. [React] How to use a setState Updater Function with a Reducer Pattern

    In this lesson we'll walk through setting up an updater function that can receive an action argument ...

  3. React Hooks vs React Class vs React Function All In One

    React Hooks vs React Class vs React Function All In One React Component Types React Hooks Component ...

  4. react新特性 react hooks

    本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...

  5. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  6. [react] 细数 React 的原罪

    Props & onChange 的原罪 .「props & onChange 接口规范」它不是一个典型的「程序接口规范」. 当你拿到一个可视组件的 ref,却没有类似 setProp ...

  7. [React] 从零开始的react

    组件 1. 无状态组件 在React中,组件的名字必须用大写字母开头,而包含该组件定义的文件名也应该是大写字母(便于区分,也可以不是). 无状态组件是纯展示组件,仅仅只是用于数据的展示,只根据传入的p ...

  8. React文档(二十三)Web Components

    React和web components是为了解决不同问题而创立的.web components为可重用组件提供了健壮的封装,而React提供了声明式的库来保持DOM和数据同步.这两点是互相补充的.作 ...

  9. React.createClass 、React.createElement、Component

    react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...

随机推荐

  1. Response的应用

    1.HttpServletResponse概述 service方法中的response的类型是ServletResponse,而doGet/doPost方法的response的类型是HttpServl ...

  2. 链表习题(6)-链表返回倒数第k个数的位置的值

    /*链表返回倒数第k个数的位置的值*/ /* 算法思想:先取得链表的长度len,之后获取len-k+1的位置元素的值 */ Elemtype Getelem_rear(LinkList L, int ...

  3. [LOJ6433] [PKUSC2018] 最大前缀和

    题目链接 LOJ:https://loj.ac/problem/6433 Solution 注意到最大前缀要满足什么性质,假设序列\(a[1..n]\)的最大前缀是\(s_x\),那么显然要满足所有\ ...

  4. 2.2_Database Interface:ODBC基本概念

    一.无ODBC时代 一般来讲不同的数据库厂商都有自己的数据库开发包,这些开发包支持两种模式的数据库开发; 1.预编译的嵌入模式(例如Oracle的ProC,SQL Server的ESQL) 2.API ...

  5. 5G能带来什么改变-从鸿蒙OS说起

    背景 从5G投票事件开始,开始关注5G.许多文章都说到5G的特点有速度快.时延低,其中,时延低是最重要的特点.然而,时延低能给社会带来什么改变呢? 2G是短信的时代,3G促成了语音视频,4G促成了短视 ...

  6. Ubuntu-18.04 LTS UEFI 安装U盘制作

    要把U盘作为UEFI启动盘,第一个分区要为FAT32分区,EFI程序放在/EFI/Boot/bootx64.efi.为了制作Ubuntu-18.04 LTS安装U盘,可以把一个U盘格式化为FAT32格 ...

  7. Android架构组件——ViewModel

    概述 ViewModel,从字面上理解的话,它肯定是跟视图(View)以及数据(Model)相关的.正像它字面意思一样,它是负责准备和管理和UI组件(Fragment/Activity)相关的数据类, ...

  8. curl 获取状态返回码

    [root@1708mode ~]# curl -o /dev/null -s -w "%{http_code}\n" baidu.com200 朋友问,就有了 wget 没学会& ...

  9. Android GOT Hook

    最后介绍的这种hook方式原理比较简单,只需要将GOT表中的目标函数地址替换为我们自己的函数地址即可,但它的缺点是只能对导入函数进行hook,还需要对elf文件的结构有所了解. 一.获取到GOT表在内 ...

  10. 小顶堆第二弹-----堆降序排序(C语言非递归)

    现在po一下C语言版本的,留作以后接口使用. 1 #include <stdio.h> #include <stdlib.h> #define HEAP_SIZE 100 #d ...