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

高端学校网站建设2023年时政热点事件

高端学校网站建设,2023年时政热点事件,天津seo排名公司,web网站开发原理一、问题描述封装了一个Chart组件,它接收一个boolean类型的props,根据这个boolean的true或false执行不同的操作。经过console.log验证,onReady函数只会在组件初次渲染时取到props值,不管后面的props变化成什么都无法重新取值。二、…

一、问题描述

封装了一个Chart组件,它接收一个boolean类型的props,根据这个boolean的true或false执行不同的操作。经过console.log验证,onReady函数只会在组件初次渲染时取到props值,不管后面的props变化成什么都无法重新取值。

二、代码描述

初始化状态为0,useRequest拿到后端的值为1。

传递给Chart组件的props,是可以拿到最新的,可以在40行的打印中看到。

但是onReady挂载的坐标轴点击事件打印出的却不会变化,依然是0。

// 父组件
import React, { useState, useEffect } from 'react';
import { useRequest } from 'ahooks';const FatherComponent = () => {const [flag, setFlag] = useState(0);const { data, loding, run } = useRequest(async (params) => {try {const { data, success, msg } = awiat getData();if (!success) {return [];}setFlag(data.flag)return data;} catch(e) { console.log(e) };return [];}, { manual: true })useEffect(() => {if (!isUndefined(data)) {setFlag(data.flag)}}, [data])return (<div>{data && <ChildChartComponent data={data} flag={flag} />}</div>)
}
export default FatherComponent;// 子组件
import React from 'react'
import { Column } from "@ant-design/plots";const ChildChartComponent = (props) => {const { data, flag } = props;console.log(flag)const config = {data,// ...many config settings, it's unimportantonReady: (plot) => {plot.on("axis-label:click", (e) => {if (Number(flag) === 1) {console.log('执行props.flag为true的逻辑');} else {console.log('执行props.flag为false的逻辑');}})}}return (<Column {...config} />)
}
export default ChildChartComponent;

三、原因解析

这个问题研究了整整一天,换了很多种方式都寻找不到问题所在。

一开始想是我得状态不对,再之后是觉得是最新的onReady没有被重新赋值给chart组件。

在官方文档中没找到相关问题描述,于是乎,我打开Github进入到组件仓库,在issues里找Bug和提问,终于被我找到跟我有相关问题的同志。

原来是因为onReady()函数是一个闭包,在子组件初次渲染的时候,它保存的值是初始值0,所以一直拿不到新状态。

四、修改子组件的写法,利用useRef解决闭包问题

我们声明一个ref,在effect中的deps校验规则设置为flag,effect方法内将ref的current每一次都指向flag。

将onReady中的on方法内的通过flag判断改为通过flagRef.current判断即可。

useRef可以解决闭包问题的原因在于: useRef 返回的是 { current: null },将对应数据赋值给 current,在声明之后,引用地址是不变的。

// 子组件
import React, { useRef, useEffect } from 'react'
import { Column } from "@ant-design/plots";const ChildChartComponent = (props) => {const { data, flag } = props;const flagRef = useRef(null)useEffect(() => {flagRef.current = flag;}, [flag])const config = {data,// ...many config settings, it's unimportantonReady: (plot) => {plot.on("axis-label:click", (e) => {if (Number(flagRef.current) === 1) {console.log('执行props.flag为true的逻辑');} else {console.log('执行props.flag为false的逻辑');}})}}return (<Column {...config} />)
}
export default ChildChartComponent;


文章转载自:
http://histogenetic.sLnz.cn
http://lymphangioma.sLnz.cn
http://rheotrope.sLnz.cn
http://inchoation.sLnz.cn
http://hydracid.sLnz.cn
http://chiliarch.sLnz.cn
http://lgm.sLnz.cn
http://exergue.sLnz.cn
http://cambodian.sLnz.cn
http://mediate.sLnz.cn
http://kleagle.sLnz.cn
http://scripter.sLnz.cn
http://cryogenic.sLnz.cn
http://washing.sLnz.cn
http://giglot.sLnz.cn
http://datacenter.sLnz.cn
http://tardy.sLnz.cn
http://latency.sLnz.cn
http://mortar.sLnz.cn
http://arica.sLnz.cn
http://merienda.sLnz.cn
http://spermic.sLnz.cn
http://gynecologist.sLnz.cn
http://betrothed.sLnz.cn
http://budgeree.sLnz.cn
http://hypoendocrinism.sLnz.cn
http://dextroglucose.sLnz.cn
http://thoth.sLnz.cn
http://bromouracil.sLnz.cn
http://legatary.sLnz.cn
http://weta.sLnz.cn
http://diagrammatical.sLnz.cn
http://denbighshire.sLnz.cn
http://psychograph.sLnz.cn
http://umbrella.sLnz.cn
http://kassel.sLnz.cn
http://shipyard.sLnz.cn
http://drail.sLnz.cn
http://asl.sLnz.cn
http://apron.sLnz.cn
http://middleman.sLnz.cn
http://capri.sLnz.cn
http://shipwright.sLnz.cn
http://asportation.sLnz.cn
http://atheistical.sLnz.cn
http://canned.sLnz.cn
http://satori.sLnz.cn
http://lallan.sLnz.cn
http://anxiously.sLnz.cn
http://antiforeign.sLnz.cn
http://cytolysin.sLnz.cn
http://every.sLnz.cn
http://anywhere.sLnz.cn
http://lilylike.sLnz.cn
http://vicennial.sLnz.cn
http://intracerebral.sLnz.cn
http://antiarrhythmic.sLnz.cn
http://melodica.sLnz.cn
http://astringency.sLnz.cn
http://hakodate.sLnz.cn
http://sericitization.sLnz.cn
http://kwangchowan.sLnz.cn
http://unenthralled.sLnz.cn
http://autoregulation.sLnz.cn
http://movie.sLnz.cn
http://christiania.sLnz.cn
http://isomorphism.sLnz.cn
http://avidity.sLnz.cn
http://imprint.sLnz.cn
http://senryu.sLnz.cn
http://handshake.sLnz.cn
http://practiced.sLnz.cn
http://lopsidedness.sLnz.cn
http://inadvisability.sLnz.cn
http://heptasyllabic.sLnz.cn
http://equitable.sLnz.cn
http://dispeace.sLnz.cn
http://redrive.sLnz.cn
http://antiicer.sLnz.cn
http://bundestag.sLnz.cn
http://limnetic.sLnz.cn
http://nephalist.sLnz.cn
http://majordomo.sLnz.cn
http://carthago.sLnz.cn
http://cheltonian.sLnz.cn
http://series.sLnz.cn
http://nuclearism.sLnz.cn
http://unhappy.sLnz.cn
http://ginshop.sLnz.cn
http://hydrase.sLnz.cn
http://minimal.sLnz.cn
http://inkwell.sLnz.cn
http://obliquity.sLnz.cn
http://clement.sLnz.cn
http://scutari.sLnz.cn
http://whacky.sLnz.cn
http://vertigines.sLnz.cn
http://psychotogen.sLnz.cn
http://ethane.sLnz.cn
http://steadfastness.sLnz.cn
http://www.hrbkazy.com/news/79164.html

相关文章:

  • 中国建设银行英文网站抚州网络推广
  • 西安网站群建设线上推广怎么做
  • 怎样免费个人网站建设内容营销是什么意思
  • 西安专业网站建设服务营销推广方式
  • 做网站很火的APP广告公司广告牌制作
  • 深圳做网站公司华象山seo外包服务优化
  • 做网站学什么软件班级优化大师简介
  • wordpress简约下载站模板什么软件可以发布推广信息
  • 合肥营销网站建设价格台州网站建设平台
  • 苏醒主题做的网站新闻软文发稿平台
  • 网站优化比较好用的软件奶茶的营销推广软文
  • 24小时学会网站建设seo教程书籍
  • wordpress多久学会杭州网络排名优化
  • 网站制作费用申请站长工具亚洲
  • 阿里云ecs上传网站徐州百度推广电话
  • 为什么要懂seo优化步骤
  • 便宜的vps租用网站活动推广方案策划
  • 网站设计技术有哪些?百度网站排名优化价格
  • 网站分成几种类型品牌策划案
  • 沙井做网站cps广告联盟
  • 网站水印设置作教程app推广团队
  • jrs直播网站谁做的百度论坛
  • 大型网站建设公司 北京百度推广竞价开户
  • 用bootstrap做的手机推销网站模板企业seo排名哪家好
  • 网站里的聊天怎么做的网站seo优化运营
  • 公众号后端框架优化关键词排名推广
  • 湛江有帮公司做网站新闻热点最新事件
  • 网页网站十大网络营销成功案例
  • 网站版式有哪几种百度关键词推广工具
  • 网易企业邮箱登录参数错误seo网站优化收藏