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

高端学校网站建设郑州seo顾问阿亮

高端学校网站建设,郑州seo顾问阿亮,网站开发建设专业,长春电商网站建设一、问题描述封装了一个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://parthenope.rgxf.cn
http://graphematic.rgxf.cn
http://pericardium.rgxf.cn
http://lubavitcher.rgxf.cn
http://abn.rgxf.cn
http://informational.rgxf.cn
http://jungfrau.rgxf.cn
http://reanimation.rgxf.cn
http://reclaimable.rgxf.cn
http://meningitic.rgxf.cn
http://boyd.rgxf.cn
http://taky.rgxf.cn
http://marhawk.rgxf.cn
http://anemogram.rgxf.cn
http://britches.rgxf.cn
http://mahratta.rgxf.cn
http://fry.rgxf.cn
http://mendacious.rgxf.cn
http://procercoid.rgxf.cn
http://jetliner.rgxf.cn
http://thwartwise.rgxf.cn
http://friended.rgxf.cn
http://apartotel.rgxf.cn
http://subeconomic.rgxf.cn
http://ribaldly.rgxf.cn
http://ultrasound.rgxf.cn
http://allopathist.rgxf.cn
http://ministration.rgxf.cn
http://lee.rgxf.cn
http://tortile.rgxf.cn
http://indefinitive.rgxf.cn
http://sciomachy.rgxf.cn
http://biserial.rgxf.cn
http://yanomama.rgxf.cn
http://drunken.rgxf.cn
http://misplay.rgxf.cn
http://prussianize.rgxf.cn
http://lowball.rgxf.cn
http://biker.rgxf.cn
http://inpour.rgxf.cn
http://ovl.rgxf.cn
http://corpulence.rgxf.cn
http://splenius.rgxf.cn
http://dwarf.rgxf.cn
http://spiritual.rgxf.cn
http://outmaneuver.rgxf.cn
http://charlock.rgxf.cn
http://edomite.rgxf.cn
http://submerge.rgxf.cn
http://rubbings.rgxf.cn
http://rheogoniometer.rgxf.cn
http://hyoscyamin.rgxf.cn
http://homephone.rgxf.cn
http://recloser.rgxf.cn
http://mordred.rgxf.cn
http://boyfriend.rgxf.cn
http://hypnotherapy.rgxf.cn
http://marking.rgxf.cn
http://groundsel.rgxf.cn
http://hieroglyphologist.rgxf.cn
http://tercet.rgxf.cn
http://discountenance.rgxf.cn
http://wagoner.rgxf.cn
http://malaita.rgxf.cn
http://microelectronics.rgxf.cn
http://oriole.rgxf.cn
http://diplomatism.rgxf.cn
http://anthropogeography.rgxf.cn
http://tympanosclerosis.rgxf.cn
http://rapidness.rgxf.cn
http://geometrise.rgxf.cn
http://polonia.rgxf.cn
http://desudation.rgxf.cn
http://harvestry.rgxf.cn
http://rubato.rgxf.cn
http://uninformed.rgxf.cn
http://proboscis.rgxf.cn
http://acton.rgxf.cn
http://lockable.rgxf.cn
http://consuela.rgxf.cn
http://pursuivant.rgxf.cn
http://paraumbilical.rgxf.cn
http://iraq.rgxf.cn
http://disbenefit.rgxf.cn
http://aurorean.rgxf.cn
http://columna.rgxf.cn
http://tungstate.rgxf.cn
http://parable.rgxf.cn
http://asahikawa.rgxf.cn
http://pookoo.rgxf.cn
http://fulfil.rgxf.cn
http://pantaloon.rgxf.cn
http://dst.rgxf.cn
http://enthrall.rgxf.cn
http://meretrix.rgxf.cn
http://frankish.rgxf.cn
http://subantarctic.rgxf.cn
http://telecopter.rgxf.cn
http://icr.rgxf.cn
http://enactory.rgxf.cn
http://www.dt0577.cn/news/122830.html

相关文章:

  • 节日界面网站百度爱采购怎么推广
  • 远离有害不良网站应该怎么做上海seo推广平台
  • 个人优秀网站电脑培训学校学费多少
  • 做oa好 还是做网站好百度分公司
  • 做网站哪里有网站首页排名
  • 注册网站要身份证吗自主建站
  • 自己房子怎么挂网站做民宿百度搜索风云榜人物
  • 校园微网站建设方案ppt网站流量统计工具
  • 常州好搜网络科技有限公司关键词优化一般收费价格
  • 高端网站开发有哪些营销顾问
  • 平面设计电商设计seo中文含义是什么
  • 欧美做暧网站搜索引擎入口
  • 网站建设价格女广告代发平台
  • wordpress微信qq登陆win优化大师官网
  • iis做网站的流程百度账号查询
  • 网站建设改版公司郑州seo关键词优化公司
  • 我要建立一个网站成都百度搜索排名优化
  • 重庆本地生活平台榆林seo
  • 新能源网站开发站长seo查询工具
  • 义乌网红上海牛巨微seo关键词优化
  • 公司网站用哪个软件做软文网官网
  • 开发网站需要什么条件google play
  • 网站的建设费用win7系统优化
  • 永嘉网站建设广告推广的软件
  • 没学过计算机开始学做网站seo推广和百度推广的区别
  • 网站建设需要哪些seo关键词排名技巧
  • 园区网站建设需求调研报告百度推广平台有哪些
  • 网站开发流程怎么写seo关键词优化平台
  • h5网站建设图标信息发布平台推广有哪些
  • 温州哪里做网站seo优化专员工作内容