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."

  1. import React, { useState, useEffect } from 'react'
  2. import { Form, Label, Textarea, Button, Title } from './Feedback.styles'
  3.  
  4. export function FeedbackEffectComponent() {
  5. const [text, setText] = useState('')
  6. useEffect(() => {
  7. async function getStarWarsQuote() {
  8. // Get initial text
  9. const response = await fetch(
  10. 'https://starwars-quote-proxy-gi0d3x1lz.now.sh/api/randomQuote'
  11. )
  12. const data = await response.json()
  13. const quote = data.starWarsQuote
  14. setText(quote)
  15. }
  16. getStarWarsQuote()
  17. }, [])
  18.  
  19. // Handle form submission
  20. function handleSubmit(e) {
  21. e.preventDefault()
  22. console.log(`Submitting response to API: "${text}"`)
  23. setText('')
  24. }
  25.  
  26. // Update text in state onchange for textarea
  27. function handleTextChange(e) {
  28. const updatedText = e.target.value
  29.  
  30. setText(updatedText)
  31. }
  32.  
  33. return (
  34. <Form onSubmit={e => handleSubmit(e)}>
  35. <Title>Effect Example</Title>
  36. <Label>
  37. Have feedback for our team? <br /> Let us know here
  38. [React] Use the React Effect Hook in Function Components的更多相关文章

      1. [React] Write a Custom React Effect Hook
      1. Similar to writing a custom State Hook, well write our own Effect Hook called useStarWarsQuote, whi ...

      1. [React] How to use a setState Updater Function with a Reducer Pattern
      1. In this lesson we'll walk through setting up an updater function that can receive an action argument ...

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

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

      1. React.js Tutorial: React Component Lifecycle
      1. Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

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

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

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

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

    1.  
    2. 随机推荐

        1. c++模板使用及常见问题
        1. 一.为什么使用模板?? 使用模板的目的是为了避免重复声明和定义一系列基本功能相同的函数或者类,其区别因传入参数的不同而产生不同类型的数据,其基本工作过程都是一致的! 二.调用模板函数产生不明确问题 ( ...

        1. Qt 自定义QTabWidget
        1. 思路: QTabWidget的特点:点击不同的选项卡显示不同的窗口.可以将QTabWidget分成两部分: (1).选项卡:点击时要知道点击的是哪个选项.则需要将选项卡和窗口的信息存起来,点击时去这个 ...

        1. mysql sql 分析
        1. 一.SQL 执行时间分析通过找到执行时间长的 SQL 语句,可以直观的发现数据层的效率问题. 1.通过 show processlist 来查看系统的执行情况mysql> show proces ...

        1. python MySQL 插入Elasticsearch
        1. 一.需求分析 注意: 本环境使用 elasticsearch 7.0版本开发,切勿低于此版本 mysql 表结构 有一张表,记录的数据特别的多,需要将7天前的记录,插入到Elasticsearch中, ...

        1. [洛谷P2056][ZJOI2007]捉迷藏(2019-7-20考试)
        1. 题目大意:有一棵$n(n\leqslant10^6)$个点的树,上面所有点是黑点,有$m$次操作: $C\;u$:把点$u$颜色翻转 $G$:问树上最远的两个黑点的距离,若没有黑点输出$0$ 题解:有 ...

        1. Python爬虫快速上手教程
        1. 1 这个是什么        整理Python中requests常用的API 2 代码 from bs4 import BeautifulSoup import requests import re ...

        1. redis订阅与发布系统
        1. 一.概述 1.redis通过publish.subscribe等命令实现了订阅与发布模式. 2.这个功能提供两种信息机制,分别是订阅/发布到频道和订阅/发布到模式. 二.频道的订阅与信息发送 1.re ...

        1. 阿里云ECS服务器将默认的Ubuntu系统改成桌面版
        1. 以Ubuntu14.04 64位 为例 1.用自己PC登录阿里云,停止正在运行的实例 2.重装系统 更换系统盘->选择"公共镜像".Ubuntu. Ubuntu14.04 6 ...

        1. C++ | 使用const std::map,map::[]时遇到的一个bug
        1. 原函数简化后如下: void fun(const map<int,vector<int>> &mp, int index) { for (auto tmp : mp[i ...

        1. 定时任务 Quarzt
        1. Quartz.Net是一个从java版的Quartz移植过来的定时任务框架,可以实现异常灵活的定时任务. Quartz 有三个概念分别是 计划者(ISchedeler).工作(IJob).触发器(Tr ...