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

做外贸什么网站阿里巴巴招聘官网

做外贸什么网站,阿里巴巴招聘官网,网页qq邮箱,可以做网站吗目录 1,问题描述2,解决方式2.1,Render Props2.2,HOC 3,使用场景 1,问题描述 当使用组件时,标签中的内容,会被当做 props.children 来渲染: 子组件: import…

目录

  • 1,问题描述
  • 2,解决方式
    • 2.1,Render Props
    • 2.2,HOC
  • 3,使用场景

1,问题描述

当使用组件时,标签中的内容,会被当做 props.children 来渲染:

子组件:

import React, { PureComponent } from "react";export default class MyComponent extends PureComponent {state = {x: 0,y: 0,};render() {return <div>{this.props.children}</div>;}
}

父组件使用:

import React from "react";
import MyComponent from "./MyComponent";export default function index() {return (<div><MyComponent><h2>1次使用</h2></MyComponent><MyComponent><h2>2次使用</h2></MyComponent></div>);
}

现在的需求:父组件中如何使用子组件的状态?

2,解决方式

2.1,Render Props

注意到在子组件中默认渲染的是 props.children,那如果像上下文 <ctx.Consumer></ctx.Consumer> 一样,通过函数参数来传递指定内容,就可以解决了。

所以,props.children 变成函数即可

import React, { PureComponent } from "react";export default class MyComponent extends PureComponent {state = {x: 1,y: 2,};render() {return <div>{this.props.children(this.state)}</div>;}
}

使用

import React from "react";
import MyComponent from "./MyComponent";export default function index() {return (<div><MyComponent>{(childrenState) => <h2>{childrenState.x}</h2>}</MyComponent><MyComponent>{(childrenState) => <h2>{childrenState.y}</h2>}</MyComponent></div>);
}

另外,一般这种情况不会用 props.children,而是使用约定俗成的 props.render 来表示。

// 子组件
<div>{this.props.render(this.state)}</div>;// 父组件
<MyComponent render={(childrenState) => <h2>{childrenState.x}</h2>} />

注意,因为子组件 extends PureComponent,所以父组件应该将这个函数单独声明才行,否则每次都会重新渲染。(具体原因看这篇文章)

修改父组件如下:

import React from "react";
import MyComponent from "./MyComponent";const renderA = (childrenState) => <h2>{childrenState.x}</h2>;export default function index() {return (<div><MyComponent render={renderA}></MyComponent></div>);
}

2.2,HOC

高阶组件也能解决这个问题,但相比 Render props 有点繁琐。

import React, { PureComponent } from "react";export default function withState(Comp) {return class MyComponent extends PureComponent {state = {x: 1,y: 2,};render() {return <Comp {...this.props} x={this.state.x} />;}};
}

父组件使用

import React from "react";
import withState from "./withState";function ChildA(props) {return <h2>{props.x}</h2>;
}const ChildStateA = withState(ChildA);export default function index() {return (<div><ChildStateA /></div>);
}

3,使用场景

可以看到,效果类似 vue中作用域插槽 。

所以大多的使用场景:某些组件的各个功能和处理逻辑相同,只是渲染的UI不同


以上。

http://www.yayakq.cn/news/484528/

相关文章:

  • 云建网站石家庄有没有销售做被用的网站
  • windows 2008 iis添加网站七牛云动静分离wordpress
  • 张家界做网站网站品牌建设公司
  • 域名备案中网站可以开通制作网站一般使用的软件有哪些
  • 网站建站 外贸江门网站推广哪里专业
  • 做婚礼效果图的网站有哪些wordpress 进后台
  • 长垣县住房和城乡建设局网站如何做家教网站
  • 如何申请网站优化工作广州企业官网建设
  • 专业的建网站的公司wordpress点评模板
  • 会员管理网站ASP建设做网站哪里最便宜
  • 杭州高端网站制作基于 seajs 的高性能网站开发和优化实践_王保平(淘宝)
  • 石药网站好一点的app开发公司
  • 风景旅游网站建设的设计思路视频拍摄剪辑培训
  • 学生个人网站模板嘉峪关建设局网站
  • 专业构建网站的公司wordpress 游戏 模板下载
  • 哪个博客可以做单页网站台山住房和城乡建设 网站
  • 搭建网站的五大步骤seo优化seo外包
  • 网站内容编辑器上海网络推广报价
  • 网站备案多长时间来完成简单的做海报的网站
  • 做点心的网站手机网站有什么
  • 游戏发号网站源码快速做网站详情页
  • 苏州做网站公司精选苏州聚尚网络更改wordpress后台登录地址
  • 营销网站建设内容wordpress新建文章页修改图片样式
  • 锦州网站建设公司公司做网站价格
  • 廊坊网站建站贺州招聘网站建设
  • 嘉定建设机械网站论述网站建设流程
  • 网站程序风格淘宝网站建设目的
  • 未央网站建设pc做网站
  • 网站配色原理wordpress教程破解版
  • cdn网站加速有用吗兄弟网站制作