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

学院做网站的意义厦门网站到首页排名

学院做网站的意义,厦门网站到首页排名,怎么编辑网站源码,中国标准物质信息网网站建设一、React Context 原理 简单地说就是可以将一些数据注入到Context对象中,使其下辖的组件可以随时随地访问这些数据,省去了逐层传递的步骤。 相对于在组件里挖槽(比如{props.children}),使用Context应该更注重随时随…

一、React Context 原理

简单地说就是可以将一些数据注入到Context对象中,使其下辖的组件可以随时随地访问这些数据,省去了逐层传递的步骤。

相对于在组件里挖槽(比如{props.children}),使用Context应该更注重随时随地都可能有需求使用这些数据这个目的。

核心API:

  • React.createContext(defaultValue)
    创建Context对象,可传入defaultValue或undefined。                                                即:当某个组件订阅了Context但未匹配到Provider时,就会访问defaultValue值,当传入undefined时,defaultValue无效。(defaultValue永远不会发生改变。当 React 无法找到匹配的 provider 时,该值会被作为后备方案。)
  • Context.Provider 
    Context对象都会返回Provider组件,其下的组件会订阅Provider中的数据。Provider接收value属性,用于将value传递给消费组件,当value发生变化时所有消费组件都会重新渲染。

二、使用

// AppContext.js
import React from 'react';
const defaultValue = null
const AppContext = React.createContext(defaultValue);
export default AppContext;
// app.ts
import AppContext from './appContext';class App extends Component<any, any> {state: any = {baseConfig: {value: 123}}//要渲染的页面render() {return (<AppContext.Provider value={this.state.baseConfig}>{this.props.children}</AppContext.Provider>)}
}export default App
// 自组件使用
export function Component(props: any) {return <AppContext.Consumer>{(value) => <Customer_page_overview_data {...props} {...value} />}</AppContext.Consumer>
}

三、异步传递问题

异步获取的数据存储全局的context中,子组件中使用这个context的数据时,数据为空;

解决: 

componentDidUpdate方法里异步获取数据;

每次this.setState()都会触发componentDidUpdate这个方法。因此我们在componentDidUpdate 中使用 this.setState() 就会无限循环。所以我们使用if来进行控制。

// app.ts
import AppContext from './appContext';class App extends Component<any, any> {state: any = {baseConfig: {value: 123}}componentDidUpdate() {if (!this.state.baseConfig.value) {API_CONFIG.getLogo({queryParams: {var: "report.baseConfig.target"}}).then(baseConfig => {console.log(baseConfig, "-=-=--=-=-AppContext=-=-=-=")this.setState({baseConfig})})}}//要渲染的页面render() {return (<AppContext.Provider value={this.state.baseConfig}>{this.props.children}</AppContext.Provider>)}
}export default App

http://www.hkea.cn/news/827546/

相关文章:

  • 网站彩票投注员做啥的真正免费的网站建站平台运营
  • wordpress 中文注册鸡西seo
  • 佛山企业如何建网站seo的内容怎么优化
  • 在什么网站上做自媒体windows优化大师是自带的吗
  • 装修公司的网站怎么做第三方营销平台有哪些
  • 百度公司做网站吗手机网页链接制作
  • 武汉移动网站制作今天新闻最新消息
  • 酒泉建设厅网站百度seo刷排名软件
  • 天津个人网站建设yandex引擎
  • 网站改版建设 有哪些内容网络营销策划方案怎么做
  • 网站建设拾金手指下拉seo的实现方式
  • 北京宣传片湖南seo优化哪家好
  • 下载app 的网站 如何做黑帽seo排名技术
  • 个人是否做众筹网站哪里可以免费推广广告
  • 外贸网站该怎么做青岛百度推广优化怎么做的
  • 网站建设中 网页代码优化关键词排名公司
  • 网站标题优化怎么做泉州百度首页优化
  • 学习网站建设的是什么专业优化网站排名公司
  • 固定ip做网站西安网站建设推广
  • 做响应式网站好不好软文发布门户网站
  • 重庆做网站建设的公司哪家好最基本的网站设计
  • 长春网站制作wang网站营销软文
  • discuz 网站搬家市场营销的策划方案
  • 做婚礼网站的公司简介seo网站关键词优化软件
  • 哪些客户需要做网站推广平台排名前十名
  • 团购的网站扣佣金分录怎么做厦门百度竞价
  • 国家疫情最新政策麒麟seo外推软件
  • 河南第二波疫情最新消息淘宝关键词优化技巧教程
  • 优化好的网站做企业网站百度代理公司
  • 外贸b2c网站如何做推广百度电话人工服务