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

最新文章

  1. pt_table_checksum对检查表的chunk大小的限制
  2. 强连通 HDU 3639
  3. Leetcode Combination Sum
  4. jQuery 屏蔽鼠标快速经过
  5. Allegro中板子边框不封闭导致的z-copy无法用的问题
  6. java 进制转化
  7. css3学习笔记之多列
  8. 在virtual pc中搭建基于ubuntu 的git环境
  9. Windows端口转发
  10. php基础(六)Include
  11. 【转】动态字节码技术跟踪Java程序
  12. .NET Core+MySql+Nginx 容器化部署
  13. 《设计模式之禅》--设计模式大PK
  14. 在Debian/Ubuntu系统中安装*.sh与*.bin文件
  15. SAP MM Storage Location Missing in MD04 Result?
  16. OpenStack若干概念
  17. POI导出Word插入复选框
  18. 关于java内存泄露的总结--引用的类型:强引用,弱引用,软引用
  19. js一些常用方法总结
  20. HCNP学习笔记之史上最全华为路由器交换机配置命令大合集

热门文章

  1. 阿里的Netty知识点你又了解多少
  2. 使用Python快速实现简单的人脸检测
  3. 在bat批处理中简单的延时方法
  4. logrus 剖析之滚动日志
  5. TweenLite参数用法中文介绍
  6. Js学习03--数据类型
  7. 全栈项目|小书架|服务器开发-Koa2中间件机制洋葱模型了解一下
  8. 解析:让你弄懂redux原理
  9. springboot读取系统级环境变量,和读写系统属性以及unittest来获取环境变量的方法
  10. java第三次面试总结