[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 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的更多相关文章
- [React] Write a Custom React Effect Hook
Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...
- [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 ...
- 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 ...
- react新特性 react hooks
本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...
- React.js Tutorial: React Component Lifecycle
Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...
- [react] 细数 React 的原罪
Props & onChange 的原罪 .「props & onChange 接口规范」它不是一个典型的「程序接口规范」. 当你拿到一个可视组件的 ref,却没有类似 setProp ...
- [React] 从零开始的react
组件 1. 无状态组件 在React中,组件的名字必须用大写字母开头,而包含该组件定义的文件名也应该是大写字母(便于区分,也可以不是). 无状态组件是纯展示组件,仅仅只是用于数据的展示,只根据传入的p ...
- React文档(二十三)Web Components
React和web components是为了解决不同问题而创立的.web components为可重用组件提供了健壮的封装,而React提供了声明式的库来保持DOM和数据同步.这两点是互相补充的.作 ...
- React.createClass 、React.createElement、Component
react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...
随机推荐
- mysql5.5免安装版配置教程
1. 下载免安装版 ,解压缩 2. 配置MYSQL的环境变量 新增系统变量MYSQL_HOME:安装目录 在PATH变量的最后面添加: ;%MYSQL_HOME%\bin(注意前面的:) 3. 打开文 ...
- Python小知识点+保留字
注意 !/usr/bin/python # -- coding: UTF-8 -- #中文编码 Python空行:函数之间或类的方法之间用空行分隔,表示一段新的代码的开始 Python注释:单行注释采 ...
- go 疑难杂症
func Test_doSeond(t *testing.T) { msg := make([]Msg, 0) for i := 0; i < 5; i++ { m := Msg{ data: ...
- 从docker中备份oracle和mongo数据
从docker中导出Oracle数据 这里推荐先把脚本文件放到容器里面(这里没有) #!/bin/sh # 进入容器 # 本机备份位置 /root/oracleData/dist/temp # 当前日 ...
- Selenium 配置IE浏览器
1.安装selenium pip install selenium 2.安装IE浏览器driver http://selenium-release.storage.googleapis.com/ind ...
- go liteIDE 快捷键
Goland常用快捷键文件相关快捷键: CTRL+E,打开最近浏览过的文件.CTRL+SHIFT+E,打开最近更改的文件.CTRL+N,可以快速打开struct结构体.CTRL+SHIFT+N,可以快 ...
- ThreadPoolExecutor使用错误导致死锁
背景 10月2号凌晨12:08收到报警,所有请求失败,处于完全不可用状态 应用服务器共四台resin,resin之前由四台nginx做负载均衡 服务器现象及故障恢复步骤 登入服务器,观察resin进程 ...
- 和我一起,重零开始学习Ant Design Pro开发解决方案(二)部署示例项目
- isolate sqflite demo
main.dart import 'package:flutter/material.dart'; import 'demo_isolates.dart'; import 'package:rxdar ...
- JavaScript的深浅复制
JavaScript的深浅复制 为什么有深复制.浅复制? JavaScript中有两种数据类型,基本数据类型如undefined.null.boolean.number.string,另一类是Obje ...