美文网首页
React——context越级传值

React——context越级传值

作者: 小龙虾Julian | 来源:发表于2018-07-10 17:25 被阅读0次

我们都知道,在React中数据可以以流的形式自上而下的传递,当你使用组件的时候,你可以看到组件的props属性会自上而下传递。但是在有些情况下不想通过父组件的props属性一级一级的向下传递,而是希望在某一子级中直接得到上N级父组件的props中的值。那么接下来就让我们看一下通过props传值和context传值的情况

1、通过props传值
class Button extends React.Component {
  render() {
    return (
      <button style={{background: this.props.color}}>
        {this.props.children}
      </button>
    );
  }
}
 
class Message extends React.Component {
  render() {
    return (
      <div>
        {this.props.text} <Button color={this.props.color}>Delete</Button>
      </div>
    );
  }
}
 
class MessageList extends React.Component {
  render() {
    const color = "purple";
    const children = this.props.messages.map((message) =>
      <Message text={message.text} color={color} />
    );
    return <div>{children}</div>;
  }
}

过程解析:
(1)代码结构大致分为3级:顶层MessageList、Message一级子类和Button底层子类
(2)从父组件到子组件的传值情况——text:在顶层组件MessageList中的值,传递到一级子组件Message中,并在此组件中被使用{this.props.text}
(3)从父组件到子组件的传值情况——color:顶层组件MessageList中的值,先传递到一级子组件Message中,在传递到二级子组件Button中,最后在二级子组件中被使用{this.props.color}
(4)总结:从上边分析可以看出,通过props属性进行父子组件的传值是一级一级的向下传递的

2、通过context进行值得越级传递
class Button extends React.Component {
  render() {
    return (
      <button style={{background: this.context.color}}>
        {this.props.children}
      </button>
    );
  }
}
 
Button.contextTypes = {
  color: React.PropTypes.string
};
 
class Message extends React.Component {
  render() {
    return (
      <div>
        {this.props.text} <Button>Delete</Button>
      </div>
    );
  }
}
 
class MessageList extends React.Component {
  getChildContext() {
    return {color: "purple"};
  }
 
  render() {
    const children = this.props.messages.map((message) =>
      <Message text={message.text} />
    );
    return <div>{children}</div>;
  }
}
 
MessageList.childContextTypes = {
  color: React.PropTypes.string
};

过程解析:通过添加childContextTypes和getChildContext()到MessageList(context的提供者),React自动向下传递数据然后在组件树中的任意组件(也就是说任意子组件,在上面的例子中就是Button)都能通过定义contextTypes访问context中的数据。
(1)在顶层组件MessageList中定义了顶层组件所拥有的子类context对象——该顶层组件所拥有的子类context对象为color,且必须为字符串,代码如下:

MessageList.childContextTypes = {
    color: React.PropTypes.string
};

(2)通过getChldText方法,来给子context对象的属性赋值,这样就完成了顶层组件中,context组件的赋值,代码如下:

getChildContext() {
    return {color: "purple"};
}

(3)越级传递。因为color属性只在最底层使用(在一级子组件Message中并没有直接用到),因此可以直接传递到最底层(越级),在Button组件中使用。首先在Button组件中,再次说声明了所接受到的context的子组件color的类型,声明必须为字符串,代码如下:

Button.contextTypes = {
    color: React.PropTypes.string
};

(4)最后通过this.context.color这种方式调用,这样就通过Context实现了组件之间的越级传值,代码如下:

 <button style={{background: this.context.color}}>
     {this.props.children}
 </button>
3、总结:指定数据并要将数据传递下去的父组件要定义childContextTypes和getChildContext();想要接收到数据的子组件必须定义contextTypes来使用传递过来的context

相关文章

  • React——context越级传值

    我们都知道,在React中数据可以以流的形式自上而下的传递,当你使用组件的时候,你可以看到组件的props属性会自...

  • React Context传值

    基本理念 用原生JS来观察 将代码构建一个独立的作用域(其实就是闭包)将外部的变量传递给独立作用域的变量(能访问到...

  • react 组件之间通讯传值的多种方法实现

    react组件传值,大概有下面几种方法: props context redux react-router 路由切...

  • 父子组件传值的方式

    父组件传值给子组件方式1、props2、context(高级用法)React的上下文-Context3、组件组合(...

  • React Native Context 传值

    组件间传值一般会以props的方式,需要一级一级的传值。 通过Context的方式也可以传值,需要引入PropTy...

  • go简记-context

    比较 传值传引用基本类型都是传值 context包 主要结构context 首先context是线程安全的 创建 ...

  • React-redux

    React-redux 底层:通过高阶组件 + context跨组件传值实现,用 包裹根元素, 解决了在组件中频繁...

  • useContext

    接收一个 context 对象(React.createContext 的返回值)并返回该 context 的当前...

  • react Hooks —— 用法

    概念: 接收一个 context 对象(React.createContext 的返回值)并返回该 context...

  • Composition API的使用

    目标 父子组件传值props 和 context 祖孙组件传值provice和inject 生命周期 on**...

网友评论

      本文标题:React——context越级传值

      本文链接:https://www.haomeiwen.com/subject/ytrqpftx.html