简体   繁体   English

在React中,孙子可以访问祖父母的属性而无需父母将其传递下来吗?

[英]In React can properties of grandparents be accessed by a grandchild without the parent passing it down?

I am currently working on my first React project and I came into an issue with passing data between components. 我目前正在做我的第一个React项目,但是在组件之间传递数据时遇到了一个问题。 I am using this libraries calendar widget in order to style different days depending on the selected item in a list. 我正在使用此图书馆日历小部件,以便根据列表中的选定项目设置不同日期的样式。

https://github.com/vazco/meteor-universe-react-widgets https://github.com/vazco/meteor-universe-react-widgets

I have four interacting components that are within my tree in this structure: 在此结构中,我的树中有四个交互组件:

-----A-----
|         |
C         B
|
|
D

I am currently storing the data of a selected item in a list (Component B) from my top level component (Component A). 我当前正在将顶级组件(组件A)中选定项的数据存储在列表(组件B)中。 The component I want to pass the data to is Component D. What I want to do is create a state and pass the value to C and then on to D. Unfortunately, C is the calendar component from the library which I cant really manipulate. 我想将数据传递到的组件是组件D。我要做的是创建一个状态,然后将值传递给C,然后传递给D。不幸的是,C是我无法真正操纵的库中的日历组件。

As I need the object generated by the selection in B to be accessed by D without being able to manipulate C, Is there a way to pass values over the head of a child component? 由于我需要D能够访问B中的选择所生成的对象而又不能操纵C,因此有没有办法在子组件的头部传递值?

Code snippets: 代码段:

How Component C (calendar) is called in A, while also passing in component D (dayComponent). 如何在A中调用组件C(日历),同时如何传递组件D(dayComponent)。

<Calendar dayComponent={DayComponent}/>;

The calendar component declaration & import 日历组件声明和导入

System.import('{universe:react-widgets}').then(ReactWidgets => {

    Calendar = ReactWidgets.Calendar;
});

The Day component returns styling for the calendar days. Day组件返回日历日的样式。

 DayComponent = React.createClass({
  render() {
    var date = this.props.date
      , style = { backgroundColor: date < new Date() && '#F57B7B' }

    return (<div style={style}>
        {this.props.label}
      </div>);
  }
})

Not props, no. 不是道具,不是。 That's not how React works. 那不是React的工作方式。 React send props in a top-down direction, with parents passing properties to their children. React通过自上而下的方向发送道具,父母将属性传递给孩子。 The idea is that a component shouldn't care where it's rendered, or by what, as long as the props set for it are valid. 这个想法是,只要为组件设置的道具是有效的,该组件就不必关心它的渲染位置或渲染方式。

That being said, there is a facility for "punching a hole" (so to speak) in your component tree, to allow a child component to access data from higher up in the tree: Context 话虽这么说, 在组件树“打孔”(这么说)的设施,以允许子组件从得更高树访问数据: 上下文

However, Context is generally regarded to be an advanced feature, and only used in very specific situations where another solution just isn't viable. 但是,上下文通常被认为是高级功能, 在非常特殊的情况下使用其他解决方案才可行

From the docs page: 从文档页面:

Note: Context is an advanced and experimental feature. 注意:上下文是一项高级的实验功能。 The API is likely to change in future releases. 该API可能会在将来的版本中更改。 Most applications will never need to use context. 大多数应用程序将永远不需要使用上下文。 Especially if you are just getting started with React, you likely do not want to use context. 特别是如果您刚开始使用React,则可能不想使用上下文。 Using context will make your code harder to understand because it makes the data flow less clear. 使用上下文将使您的代码更难理解,因为它使数据流不那么清晰。 It is similar to using global variables to pass state through your application. 这类似于使用全局变量通过应用程序传递状态。 If you have to use context, use it sparingly. 如果必须使用上下文,请谨慎使用。

The idea is that in your top level component you define what Context properties you want to make available, and then a method to return the Context data: 这个想法是,在顶级组件中定义要使哪些Context属性可用,然后定义一个返回Context数据的方法:

class TopComponent extends Component {
  childContextTypes: {
    color: React.PropTypes.string
  },
  getChildContext: function() {
    return {color: "purple"};
  },
  ..
}

And then in a child component somewhere down the tree, you tell that component which Context properties you want access to: 然后在树下某处的子组件中,告诉该组件您要访问哪些Context属性:

class ChildComponent extends Component {
  contextTypes: {
    color: React.PropTypes.string
  },
  ...
}

And then those properties will be accessible via this.context property ( this.context.color for instance). 然后可以通过this.context属性(例如this.context.color )访问这些属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在React中向孙子传递带有参数的函数时出错 - Error on passing down function with argument to grandchild in React React.js:父状态值未传递到子属性中+无法获取API数据 - React.js: Parent state values not passing into child properties + Fetch API data cannot be accessed React Native-将状态从父级传递到子级和孙级 - React Native - Passing states from parent to child and to grandchild 将道具传递给孙子 React - Passing Props to grandchild React 将 Hook 传递给孙子/多个组件 - Passing Hook down to grandchild/multiple components 子级骨干视图如何在不将整个“ this”作为父类传递的情况下访问其父视图的特定属性 - How can a child backbone view get access to its parent view specific properties without passing whole 'this' as parent 在 React 中将数据从孙子传递给父母 - Pass data from grandchild to parent in React 反应:将道具从祖父母传递给孙子 - React: Passing Props from grandparent to grandchild 我可以直接在对象中选择孙值而不通过父值吗? - can I pick a grandchild value directly in object without being through parent value? 在React中的父级中触发的事件上将数据向下传递给子级 - Passing data down to child on event triggered in parent in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM