当前位置: 首页 > news >正文

网站设计杭州seo从0到1怎么做

网站设计杭州,seo从0到1怎么做,石家庄自助建站模板,宁津建设局网站React Hook是什么? React官网是这么介绍的: Hook 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性。 完全可选的 你无需重写任何已有代码就可以在一些组件中尝试 Hook。但是如果你不想,你不…

React Hook是什么?

React官网是这么介绍的: Hook 是 React 16.8 的新增特性。它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性。

完全可选的 你无需重写任何已有代码就可以在一些组件中尝试 Hook。但是如果你不想,你不必现在就去学习或使用 Hook。

100% 向后兼容的 Hook 不包含任何破坏性改动。

现在可用 Hook 已发布于 v16.8.0

没有计划从 React 中移除 class 你可以在本页底部的章节读到更多关于 Hook 的渐进策略。

Hook 不会影响你对 React 概念的理解 恰恰相反,Hook 为已知的 React 概念提供了更直接的 API:props, state,context,refs 以及生命周期。稍后我们将看到,Hook 还提供了一种更强大的方式来组合他们。

如果对react还不够了解建议先看下react官方文档,写写demo再来看文章,因为有的react基础的东西我就一笔带过不细说。
react 官方文档 https://zh-hans.reactjs.org/docs/hooks-state.html

React目前提供的Hook
hook    用途
useState    设置和改变state,代替原来的state和setState
useEffect   代替原来的生命周期,componentDidMount,componentDidUpdate 和 componentWillUnmount 的合并版
useLayoutEffect 与 useEffect 作用相同,但它会同步调用 effect
useMemo 控制组件更新条件,可根据状态变化控制方法执行,优化传值
useCallback useMemo优化传值,usecallback优化传的方法,是否更新
useRef  跟以前的ref,一样,只是更简洁了
useContext  上下文爷孙及更深组件传值
useReducer  代替原来redux里的reducer,配合useContext一起使用
useDebugValue   在 React 开发者工具中显示自定义 hook 的标签,调试使用。
useImperativeHandle 可以让你在使用 ref 时自定义暴露给父组件的实例值。
1.useState
import React from 'react';
import './App.css';
//通常的class写法,改变状态
class App extends React.Component {constructor(props){super(props)this.state = {hook:'react hook 是真的好用啊'}}changehook = () => {this.setState({hook:'我改变了react hook 的值'})}render () {const { hook } = this.statereturn(<header className="App-header">{hook}<button onClick={this.changehook}>改变hook</button></header>)}
}
export  {App}//函数式写法,改变状态
function App() {
//创建了一个叫hook的变量,sethook方法可以改变这个变量,初始值为‘react hook 是真的好用啊’const [hook, sethook] = useState("react hook 是真的好用啊");return ( <header className="App-header">{hook}{/**这里的变量和方法也是可以直接使用的 */}<button onClick={() => sethook("我改变了react hook 的值")}>改变hook</button></header>);
}
export  {App}//箭头函数的函数写法,改变状态
export const App = props => {const [hook, sethook] = useState("react hook 是真的好用啊");return (<header className="App-header">{hook}<button onClick={() => sethook("我改变了react hook 的值")}>改变hook</button></header>);
};

使用方法备注在上面的demo中
看完上面useState的对比使用,一个小的demo结构更清晰,代码更简洁,更像写js代码,运用到项目中,那岂不是美滋滋。

2.useEffect & useLayoutEffect
useEffect代替原来的生命周期,componentDidMount,componentDidUpdate 和 componentWillUnmount 的合并版
useEffect( ()=>{ return ()=>{ } } , [ ])第一个参数,是函数,默认第一次渲染和更新时都会触发,默认自带一个returnreturn一个函数表示可以再销毁之前可以处理些事情
第二个参数,数组【】,空的时候表示只执行一次,更新时不触发,里面的参数是什么,当参数变化时才会执行useEffect
useEffect可以多次使用,按照先后顺序执行
useLayoutEffect 强制useeffect的执行为同步,并且先执行useLayoutEffect内部的函数
import React, { useState, useEffect, useLayoutEffect } from 'react';//箭头函数的写法,改变状态
const UseEffect = (props) => {//创建了一个叫hook的变量,sethook方法可以改变这个变量,初始值为‘react hook 是真的好用啊’const [ hook, sethook ] = useState('react hook 是真的好用啊');const [ name ] = useState('baby张');return (<header className="UseEffect-header"><h3>UseEffect</h3><Child hook={hook} name={name} />{/**上面的变量和下面方法也是可以直接使用的 */}<button onClick={() => sethook('我改变了react hook 的值' + new Date().getTime())}>改变hook</button></header>);
};const Child = (props) => {const [ newhook, setnewhook ] = useState(props.hook);//这样写可以代替以前的componentDidMount,第二个参数为空数组,表示该useEffect只执行一次useEffect(() => {console.log('first componentDidMount');}, []);//第二个参数,数组里是hook,当hook变化时,useEffect会触发,当hook变化时,先销毁再执行第一个函数。useEffect(() => {setnewhook(props.hook + '222222222');console.log('useEffect');return () => {console.log('componentWillUnmount ');};},[ props.hook ]);//useLayoutEffect 强制useeffect的执行为同步,并且先执行useLayoutEffect内部的函数useLayoutEffect(() => {console.log('useLayoutEffect');return () => {console.log('useLayoutEffect componentWillUnmount');};},[ props.hook ]);return (<div><p>{props.name}</p>{newhook}</div>);
};export default UseEffect;

3.useMemo & useCallback
他们都可以用来优化子组件的渲染问题,或者监听子组件状态变化来处理事件,这一点在以前是很难做到的,因为shouldComponentUpdate 里能监听到是否变化,但没法控制其他的外部方法,只能返回true和false,而componentDidUpdate只能在更新后执行,所以想在渲染之前做些事情就不好搞了。
useCallback目前还不能用

import React, { useState, useMemo } from 'react';const Child = ({ age, name, children }) => {//在不用useMemo做处理的时候,只要父组件状态改变了,子组件都会渲染一次,用了useMemo可以监听某个状态name,当name变化时候执行useMemo里第一个函数console.log(age, name, children, '11111111');function namechange() {console.log(age, name, children, '22222222');return name + 'change';}{/** react 官网虽说useCallback与useMemo的功能差不多,但不知道版本问题还怎么回是,这个方法目前还不能用const memoizedCallback = useCallback(() => {console.log('useCallback')},[name],);console.log(memoizedCallback,'memoizedCallback')*/}//useMemo有两个参数,和useEffect一样,第一个参数是函数,第二个参数是个数组,用来监听某个状态不变化const changedname = useMemo(() => namechange(), [ name ]);return (<div style={{ border: '1px solid' }}><p>children:{children}</p><p>name:{name}</p><p>changed:{changedname}</p><p>age:{age}</p></div>);
};const UseMemo = () => {//useState 设置名字和年龄,并用2两个按钮改变他们,传给Child组件const [ name, setname ] = useState('baby张'); const [ age, setage ] = useState(18);return (<div><buttononClick={() => {setname('baby张' + new Date().getTime()); }}>改名字</button><buttononClick={() => {setage('年龄' + new Date().getTime());}}>改年龄</button><p>UseMemo {name}{age}</p><Child age={age} name={name}>{name}的children</Child></div>);
};export default UseMemo;

4.useRef
ref跟之前差不多,useRef创建–绑定–使用,三步走,详细看代码以及备注

import React, { useState, useRef } from 'react';const UseRef = () => {//这里useState绑定个input,关联一个状态nameconst [ name, setname ] = useState('baby张');const refvalue = useRef(null);// 先创建一个空的useReffunction addRef() {refvalue.current.value = name;   //点击按钮时候给这个ref赋值// refvalue.current = name  //这样写时,即使ref没有绑定在dom上,值依然会存在创建的ref上,并且可以使用它console.log(refvalue.current.value);}return (<div><inputdefaultValue={name}onChange={(e) => {setname(e.target.value);}}/><button onClick={addRef}>给下面插入名字</button><p>给我个UseRef名字:</p><input ref={refvalue} /></div>);
};export default UseRef;

5.useContext
之前使用过context的小伙伴一看就懂,useContext的话跟之前的context基本用法差不多,代码内有详细注释说明,创建,传值,使用

import React, { useState, useContext, createContext } from 'react';const ContextName = createContext();
//这里为了方便写博客,爷爷孙子组件都写在一个文件里,正常需要在爷爷组件和孙子组件挨个引入创建的Contextconst UseContext = () => {//这里useState创建一个状态,并按钮控制变化const [ name, setname ] = useState('baby张');return (<div><h3>UseContext 爷爷</h3><buttononClick={() => {setname('baby张' + new Date().getTime());}}>改变名字</button>{/**这里跟context用法一样,需要provider向子组件传递value值,value不一定是一个参数 */}}<ContextName.Provider value={{ name: name, age: 18 }}>{/**需要用到变量的子组件一定要写在provider中间,才能实现共享 */}<Child /></ContextName.Provider></div>);
};const Child = () => {//创建一个儿子组件,里面引入孙子组件return (<div style={{ border: '1px solid' }}>Child 儿子<ChildChild /></div>);
};const ChildChild = () => {//创建孙子组件,接受爷爷组件的状态,用useContext,获取到爷爷组件创建的ContextName的value值let childname = useContext(ContextName);return (<div style={{ border: '1px solid' }}>ChildChild 孙子<p>{childname.name}:{childname.age}</p></div>);
};export default UseContext;

6.useReducer
这里的usereducer会返回state和dispatch,通过context传递到子组件,然后直接调用state或者触发reducer,我们常用useReducer 与useContext createContext一起用,模拟reudx的传值和重新赋值操作。

import React, { useState, useReducer, useContext, createContext } from 'react';//初始化stroe的类型、初始化值、创建reducer
const ADD_COUNTER = 'ADD_COUNTER';
const initReducer = {count: 0
};
//正常的reducer编写
function reducer(state, action) {switch (action.type) {case ADD_COUNTER:return { ...state, count: state.count + 1 };default:return state;}
}const CountContext = createContext();
//上面这一段,初始化state和reducer创建context,可以单独写一个文件,这里为了方便理解,放一个文件里写了const UseReducer = () => {const [ name, setname ] = useState('baby张');//父组件里使用useReducer,第一个参数是reducer函数,第二个参数是state,返回的是state和dispashconst [ state, dispatch ] = useReducer(reducer, initReducer);return (<div>UseReducer{/* 在这里通过context,讲reducer和state传递给子组件*/}<CountContext.Provider value={{ state, dispatch, name, setname }}><Child /></CountContext.Provider></div>);
};const Child = () => {//跟正常的接受context一样,接受父组件的值,通过事件等方式触发reducer,实现redux效果const { state, dispatch, name, setname } = useContext(CountContext);function handleclick(count) {dispatch({ type: ADD_COUNTER, count: 17 });setname(count % 2 == 0 ? 'babybrother' : 'baby张');}return (<div><p>{name}今年{state.count}</p><button onClick={() => handleclick(state.count)}>长大了</button></div>);
};export default UseReducer;

文章转载自:
http://basswood.crhd.cn
http://homothermal.crhd.cn
http://fizzwater.crhd.cn
http://tenantlike.crhd.cn
http://revibrate.crhd.cn
http://electee.crhd.cn
http://quarrelsomely.crhd.cn
http://quietistic.crhd.cn
http://conscriptive.crhd.cn
http://mossycup.crhd.cn
http://pedatifid.crhd.cn
http://repave.crhd.cn
http://clicketyclack.crhd.cn
http://interpretation.crhd.cn
http://uterine.crhd.cn
http://nitroglycerine.crhd.cn
http://exoticism.crhd.cn
http://ritz.crhd.cn
http://asepticize.crhd.cn
http://sulfhydrate.crhd.cn
http://winnow.crhd.cn
http://bioscope.crhd.cn
http://unpronounced.crhd.cn
http://accouterment.crhd.cn
http://thereat.crhd.cn
http://corollaceous.crhd.cn
http://parvus.crhd.cn
http://lithograph.crhd.cn
http://flecked.crhd.cn
http://hempseed.crhd.cn
http://crookneck.crhd.cn
http://zingiber.crhd.cn
http://lincolnian.crhd.cn
http://fireman.crhd.cn
http://luciferin.crhd.cn
http://benefic.crhd.cn
http://kickback.crhd.cn
http://suberin.crhd.cn
http://fistula.crhd.cn
http://exudation.crhd.cn
http://seniti.crhd.cn
http://azrael.crhd.cn
http://mutation.crhd.cn
http://pluriglandular.crhd.cn
http://menad.crhd.cn
http://musicassette.crhd.cn
http://framboesia.crhd.cn
http://rubble.crhd.cn
http://digitizer.crhd.cn
http://padang.crhd.cn
http://base.crhd.cn
http://collegiality.crhd.cn
http://chorizo.crhd.cn
http://hypobenthos.crhd.cn
http://guadalquivir.crhd.cn
http://fetation.crhd.cn
http://stopple.crhd.cn
http://reinflame.crhd.cn
http://styx.crhd.cn
http://hauteur.crhd.cn
http://electrochronograph.crhd.cn
http://boldhearted.crhd.cn
http://decoy.crhd.cn
http://billyboy.crhd.cn
http://stale.crhd.cn
http://cleavable.crhd.cn
http://vesa.crhd.cn
http://pleiotypic.crhd.cn
http://nauch.crhd.cn
http://mesh.crhd.cn
http://domino.crhd.cn
http://preplacement.crhd.cn
http://scandaroon.crhd.cn
http://armipotence.crhd.cn
http://nasogastric.crhd.cn
http://curr.crhd.cn
http://ratch.crhd.cn
http://puckish.crhd.cn
http://discrete.crhd.cn
http://superficially.crhd.cn
http://phenolize.crhd.cn
http://cowrie.crhd.cn
http://unrecognized.crhd.cn
http://scalpriform.crhd.cn
http://vulpine.crhd.cn
http://sulphonic.crhd.cn
http://reman.crhd.cn
http://monestrous.crhd.cn
http://mimeograph.crhd.cn
http://parenthesis.crhd.cn
http://caribe.crhd.cn
http://moschatel.crhd.cn
http://multitasking.crhd.cn
http://kerb.crhd.cn
http://ooa.crhd.cn
http://convulsions.crhd.cn
http://camwood.crhd.cn
http://clochard.crhd.cn
http://implausibility.crhd.cn
http://lampern.crhd.cn
http://www.15wanjia.com/news/63904.html

相关文章:

  • 太原建站的模板青岛seo服务公司
  • 四川建设银行手机银行下载官方网站下载永州网站seo
  • wordpress模板使用教程seo优化服务
  • 图片展示型网站模板下载seo效果最好的是
  • 宁波网站建设lonoo百度收录提交网站后多久收录
  • 南京著名网站制作长沙官网seo分析
  • 软件盒子wordpress长岭网站优化公司
  • 网站建设好怎么发布西安关键词优化平台
  • 为什么做街舞网站电商网站建设价格
  • 佛山企业网站多少钱海外营销方案
  • 单页优化到首页周口seo推广
  • 郑东新区建设局网站建网站模板
  • 手把手教你用动易做网站淘宝指数转换工具
  • 扫码进入网站如何做网页模板免费html
  • 法律顾问 网站 源码武汉java培训机构排名榜
  • 网站套餐报价 模版seo排名外包
  • 杭州建设培训中心网站网站链接推广工具
  • 天津开发网站公司百度推广登录后台登录入口
  • 温州网络公司网站建设关键词优化软件有哪些
  • 电子印章在线制作生成器关键词seo
  • 谷歌生成在线网站地图seo营销推广公司
  • 怎么做代理ip网站全球网站排名查询
  • 做网站哪里好优化网站首页
  • 网站建设有哪些步骤新手怎样推销自己的产品
  • 赌博平台网站怎么做沧州搜索引擎优化
  • 广州城市职业学院门户网站软件开发app制作
  • 网站建设与维护 唐清安站内免费推广有哪些
  • 做h5好的网站新东方雅思培训价目表
  • 外贸营销网站建设公司排名seo整站优化技术培训
  • 做的好的宠物食品网站预测2025年网络营销的发展