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. 微信小程序的登入与授权

    官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 小程序登录 小程序可以通 ...

  2. 关于goquery的“non-standard import”错误

    goquery运行缺包就用get github.com\andybalholm\cascadia下到gopath,然后出现“non-standard import”错误,说明github.com\an ...

  3. Java代码生成器Easy Code

    EasyCode是基于IntelliJ IDEA开发的代码生成插件,支持自定义任意模板(Java,html,js,xml).只要是与数据库相关的代码都可以通过自定义模板来生成.支持数据库类型与java ...

  4. gensim快速上手教程

    1 gensim是什么?        gensim是一个Python常用的的自然语言处理开发包, 主要用于词向量训练和加载词向量,以下解释其正确使用姿势. 2 正确使用姿势 from gensim. ...

  5. ConsoleLoggerExtensions.AddConsole(ILoggerFactory)已过时代码修复

    0x00.问题 netcoreapp2.2环境下, Startup.cs 代码配置如下 public void Configure(IApplicationBuilder app, IHostingE ...

  6. 翻译 API

    Request http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=love Pre 英译汉 Request http://fy ...

  7. 浅谈dubbo服务

    Dubbo分布式服 推荐大家一个画图工具:https://www.processon.com/i/572d51efe4b0c3c74981ec14 1.Dubbo是一个分布式服务框架,致力于提供高性能 ...

  8. MySQL比较时间(datetime)大小

    获取时间返回的秒数:strtotime('2019-05-10 00:00:00') 遇到一个业务功能需要将当前时间与数据库中的会议开始.结束时间进行比较,记录一下,方便下次用. 用unix_time ...

  9. Java中数组的定义,初始化和使用

    定义:数组是数据类型相同的,用一个标志符名称封装在一起的一个对象序列或基本类型数据序列(一组相同数据类型元素的集合,并且分配一块连续的内存来存储). 格式:int[] a1(常用)  或者 int a ...

  10. Python基础Day1—下

    六.Python运行 print()   打印命令,输出到屏幕上 操作: 命令提示符-->输入Python-->文件路径 若输入Python回车报错或者提示没有,则Python解释器没有安 ...