30分钟学会React Hook, Memo, Lazy
我们来学习React 16.8里的新特性。
1. 自行配置好React的环境,推荐你使用Create React APP, 你也可以下载本文档Zip解压到本地直接运行. https://github.com/yurizhang/fed-study/blob/master/my-project.zip
cd my-project yarn install
2. 在pages目录下新建test目录,我们使用这个目录来学习.在这里新建t1.js和t2.js
t1.js
/* eslint-disable no-console */
/* eslint-disable react/button-has-type */ // import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import React, {lazy, useState, useEffect} from "react"; const T2 = lazy(()=> import("./t2")); const PageHeaderWrapper=(prop)=>{
console.log("子组件刷新...");
return (
<>
<div>{prop.loading}</div>
<div>{prop.content}</div>
</>
)
}
const Memo = React.memo(PageHeaderWrapper, (prevProps, nextProps) => {
console.log(prevProps, nextProps);
return prevProps.loading === nextProps.loading
}
);
const rand=()=>{
// console.log("define rand");
const a=parseInt(Math.random()*10, 10);
if(a>=5){
return 1
}
return 0
}
const test=()=>{
const [count, setCount] = useState(1);
console.log('test 组件:',count);
useEffect(() => {
console.log('test组件:useEffect test',count);
document.title = `You clicked ${count} times`;
console.log('hello:', document.querySelector("#hello").innerHTML);
// 让我们传给useEffect的副作用函数 返回一个新的函数。这个新的函数将会在组件下一次重新渲染之后执行。
return function cleanup() {
console.log('useEffect hello:', document.querySelector("#hello").innerHTML);
console.log('test组件:useEffect return ',count);
};
}, []); // 给useEffect传第二个参数。用第二个参数来告诉react只有当这个参数的值发生改变时,才执行我们传的副作用函数(第一个参数)。
return (
<>
<Memo loading={count} content='test2' />
<div id="hello">Hell world!{count}</div>
<React.Suspense fallback="T2 loading...">
<T2 />
</React.Suspense>
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(rand)}>
Click me
</button>
</div>
</>
);
}
export default test;
t2.js 这里使用了axios,你要先安装一下,当然你也可以在你的模板文件public/index.htm里
/* eslint-disable no-console */
/* eslint-disable react/button-has-type */ import React, {useState, useEffect} from "react"; const T2=(prop)=>{ const [message, setMessage]=useState(()=>{ return 'start...';
}); function temp(){
axios.get('http://route.showapi.com/1764-1').then(response=> {
console.log(response.data.showapi_res_error);
setMessage(response.data.showapi_res_error);
})
}
useEffect( () => {
temp()
}
); // 给useEffect传第二个参数。用第二个参数来告诉react只有当这个参数的值发生改变时,才执行我们传的副作用函数(第一个参数)。
return (
<>
<div>T2. message: {message}</div>
</>
)
} export default T2;
30分钟学会React Hook, Memo, Lazy的更多相关文章
- 【grunt第二弹】30分钟学会使用grunt打包前端代码(02)
前言 上一篇博客,我们简单的介绍了grunt的使用,一些基础点没能覆盖,我们今天有必要看看一些基础知识 [grunt第一弹]30分钟学会使用grunt打包前端代码 配置任务/grunt.initCon ...
- 30分钟学会使用Spring Web Services基础开发
时隔一年终于又推出了一篇30分钟系列,上一篇<30分钟学会反向Ajax>是2016年7月的事情了.时光荏苒,岁月穿梭.虽然一直还在从事Java方面的开发工作,但是私下其实更喜欢使用C++. ...
- 30 分钟学会 Flex 布局
30 分钟学会 Flex 布局 有酒 617 人赞同了该文章 为什么我要写这一篇关于 Flex 布局的教程? 因为它十分简单灵活,区区简单几行代码就可以实现各种页面的的布局,以前我在学习页面布局的 ...
- 30分钟精通React今年最劲爆的新特性——React Hooks
你还在为该使用无状态组件(Function)还是有状态组件(Class)而烦恼吗? --拥有了hooks,你再也不需要写Class了,你的所有组件都将是Function. 你还在为搞不清使用哪个生命周 ...
- 30分钟学会XAML
1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...
- 30分钟学会如何使用Shiro
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...
- 教你30分钟学会XAML
1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...
- 30分钟学会Objective-C
注: 本文首发于我的个人博客:https://evilpan.com/2019/04/05/objc-basics/ 请原谅我的标题党.但是如果你有其他语言的学习经验,要学习Objective-C的语 ...
- 30分钟学会Docker里面开启k8s(Kubernetes)登录仪表盘(图文讲解)
前言 我们之前搭建了第一个docker项目: windows环境30分钟从0开始快速搭建第一个docker项目(带数据库交互):https://www.cnblogs.com/xiongze520/p ...
随机推荐
- Project Server 2016 RestAPI调用测试
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 快速实现CentOS7安装python-pip
1.首先检查linux有没有安装python-pip包,终端执行 pip -V [root@ network-scripts]# pip -V -bash: pip: command not foun ...
- JSP相关背景
-----------------siwuxie095 Sun Microsystems SUN 即 Stanford ...
- R: 数据结构、数据类型的描述。
################################################### 问题:数据结构..类型 18.4.27 有哪些数据结构.类型?? 各自有什么特点? 解决方案 ...
- storm shell命令源码分析-shell_submission.clj
当我们在shell里执行storm shell命令时会调用shell_submission.clj里的main函数.shell_submission.clj如下: shell_submission.c ...
- 【Qt文档阅读】Window and Dialog Widgets
Window and Dialog Widgets 没有嵌入到父控件中的控件(widget)称之为窗口(window).通常窗口带有边框和标题栏. Windows通常集成到桌面环境中,并且在某种程度上 ...
- e.key && e.which && e.keyCode
官方推荐用e.key来描述状态码,其他两种属性可能会在未来被废弃. 且key,keyCode和which 为只读属性 但是会有浏览器兼容性的问题,可以采用如下代码: let key = ''; ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)D(思维,DP,字符串)
#include<bits/stdc++.h>using namespace std;char c[2007][2007];char ans[4007];int s[2007][2007] ...
- Cogs 465. 挤牛奶
465. 挤牛奶 ★ 输入文件:milk2.in 输出文件:milk2.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 三个农民每天清晨5点起床,然后去牛棚给3 ...
- openstack RPC通信
openstack RPC通信 OpenStack 的主要组件有 Nova.Cinder.Neutron.Glance 等,分别负责云平台的计算.存储.网络资源管理.openstack 各组件之间是通 ...