简体   繁体   English

反应:仅当子级将渲染时才渲染父组件

[英]React: Render parent component only if children will render

I'm confused as to how to handle this situation in React. 我对如何在React中处理这种情况感到困惑。

I have some chart components in a set of tabs. 我在一组选项卡中有一些图表组件。 The charts will render or not depending on their permission prop. 图表是否显示取决于它们的权限。 We check if the user has access to that chart, and if not, it doesn't render. 我们检查用户是否有权访问该图表,如果没有访问权限,则该图表不会呈现。 Simple enough. 很简单。

Now I've been asked to make the parent tab hide if none of the child charts have permission, ie if none of them render, hide the tab completely. 现在,如果没有子图表具有权限,则要求我隐藏父选项卡,即,如果所有子图表均未呈现,请完全隐藏该选项卡。

Now, I want to do this properly in React, so I understand that data should flow from parent to child - the parent should decide if the child renders. 现在,我想在React中正确执行此操作,因此我知道数据应该从父级流到子级-父级应确定子级是否渲染。 But the permission ID is attached to each child chart - I would need to get each permission, check them and render only if any access to the children was authorised. 但是权限ID附加在每个子图表上-我需要获得每个许可,检查它们并仅在对孩子的任何访问权限被授权时才进行呈现。 But that way, isn't data flowing from child to parent? 但是那样,数据不是从孩子流向父母吗?

Greatly appreciate any kind of prods in the right direction. 非常感谢在正确方向上的任何产品。

Here is my take: 这是我的看法:

According to React principles, your data SHOULD flow from Parent to Child. 根据React原则,您的数据应该从父级流到子级。 Your parents should be the smart components, make decisions and children should be dumb always. 您的父母应该是聪明的组成部分,做出决定,孩子应该始终保持沉默。

So the solution to your problem is as follows. 因此,解决您的问题的方法如下。 Follow this flow 遵循此流程

Here is the hierarchy your data should follow: 这是您的数据应遵循的层次结构:

TABGROUP -> TAB(s) -> CHART(s)

Each tab should take in a list of charts to render. 每个选项卡都应包含要呈现的图表列表。 Each chart should take in appropriate props for it to render. 每个图表都应采用适当的道具来呈现。

Now before sending in the list of charts to render to each tab, you should check for the condition that at least one chart is permissible to be drawn. 现在,在发送图表列表以呈现到每个选项卡之前,您应该检查是否允许绘制至少一张图表。

Let us consider your data store returns a JSON (if it does not, you can restructure data into a similar philosophy). 让我们考虑您的数据存储返回一个JSON (如果没有,则可以将数据重组为类似的原理)。 You should fetch data before rendering (by maintaining states, you can lookup some AJAX sample if you are not sure what to do). 您应该在渲染之前获取数据(通过保持状态,如果不确定要怎么做,可以查找一些AJAX示例)。

var data = {
  'tab1': [
    {
       'id': 'chart1',
       'permission': true,
       'data': { ... }
    },
    ...
  ],
  'tab2': [
    {
       'id': 'chartn',
       'permission': false,
       'data': { ... }
    },
    ...
  ],
  ...
}

Now let us try to filter the data as follows, 现在让我们尝试如下过滤数据,

for (var tab in data) {
  data[tab] = data[tab].map(function(chart) {
     return chart.permission === true;
  });
  if (data[tab].length < 1) {
    delete data[tab];
  }
}

And now that we have filtered data, let is render our components, 现在我们已经过滤了数据,现在渲染组件,

// the variable was "data"
<TabGroup>
  {
     Object.keys(data).map(function(tab, i) {
       // tab is a list
       return <Tab key={i} charts={tab} />;
     })
  }
</TabGroup>

Now in your Tab component, 现在在您的Tab组件中,

// incoming prop was charts, so in your render function

<div>
  this.props.charts.map(function(chart, i) {
    // chart is a JSON object as above
    return <Chart key={i} chartData={chart} />;
  });
</div>

And eventually you can process the chartData in your Chart component. 最后,您可以在Chart组件中处理chartData。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM