简体   繁体   English

在中继项目中分离React组件

[英]Separating React Components in a Relay Project

I have worked with React for a while and I am very excited to learn and adopt Relay.js. 我和React一起工作了一段时间,我很高兴学习并采用Relay.js。 I am having alot of trouble finding a way to keep components reusable across multiple projects and still utilize Relay. 我很难找到一种方法来保持组件在多个项目中可重用,并仍然使用Relay。 I was able to detach components from the Relay Containers. 我能够从继电器容器中分离组件。 Which I thought was the right move. 我认为这是正确的举动。 Please see below. 请看下面。

React Component 反应组件

import React from 'react';
    class ItemList extends React.Component {
        createList(item) {

            return (
                <li id={item.node.id} key={item.node.id}>{item.node.name}</li>
            );
        }
        render() {
           let listValues = this.props.test.edges.map(this.createList);
            console.log(this.props.test);
            return (
                <div>
                    <h1>My List</h1>
                    <ul>
                        {listValues}
                    </ul>
                </div>
            );
        }
    }


    export default ItemList;

Relay Container 继电器容器

import React from 'react';
import Relay from 'react-relay';
import ItemList from './ItemList.js';
    export default Relay.createContainer(ItemList, {
        fragments: {

        },
    });

As I started to learn more about Relay I am starting to see 当我开始更多地了解接力时,我开始看到了

this.props.relay.getPendingTransactions(this.props.game)

and

this.props.relay.hasOptimisticUpdate(hidingSpot)

When Relay is used in the component level of a React Component I do not see how that component still keeps it's reusability? 当在React组件的组件级别中使用Relay时,我看不到该组件如何仍然保持其可重用性? I am still learning the relay way. 我还在学习接力方式。 I do not see how I can use a component in a styleguide and in multiple projects(another that does not implement relay). 我不知道如何在样式指南和多个项目中使用组件(另一个不实现中继)。 I could maybe use a component in multiple Relay Projects but that would be it. 我可以在多个中继项目中使用一个组件但是就是这样。

Has anyone else been faced with this issue? 还有其他人遇到过这个问题吗? When Facebook explains it in https://facebook.github.io/relay/docs/thinking-in-relay.html They make it sound as if components are loosely coupled from relay. 当Facebook在https://facebook.github.io/relay/docs/thinking-in-relay.html中解释它时,它们听起来好像组件与继电器松散耦合。 What am I missing? 我错过了什么?

React does not dictate how we should fetch data as it is a UI framework. React没有规定我们应该如何获取数据,因为它是一个UI框架。 We can fetch data imperatively (eg, using REST APIs) or declaratively (eg, using Relay). 我们可以命令性地(例如,使用REST API)或声明性地(例如,使用中继)获取数据。 If you are using Relay-specific features (eg, this.props.relay , mutation) in your React components, you made a decision of using declarative data fetching in your application and of using Relay to do so. 如果在React组件中使用特定于Relay的功能(例如, this.props.relay ,mutation),则决定在应用程序中使用声明性数据提取并使用Relay来执行此操作。 Therefore, it is not possible to reuse those in a project that does not use Relay. 因此,不能在不使用Relay的项目中重用它们。 That's the price of using a framework, IMHO. 这是使用框架的价格,恕我直言。

When Facebook explains it in https://facebook.github.io/relay/docs/thinking-in-relay.html They make it sound as if components are loosely coupled from relay. 当Facebook在https://facebook.github.io/relay/docs/thinking-in-relay.html中解释它时,它们听起来好像组件与继电器松散耦合。

It is important to understand how Relay decouples React components . 了解Relay如何解耦React组件非常重要。

Relay solves a particular problem of React. Relay解决了React的一个特殊问题 The problem arises when we have a hierarchy or tree of components and the parent component needs to know all the data requirements of its child components. 当我们有一个组件层次结构或树,并且父组件需要知道其子组件的所有数据要求时,就会出现问题。 Because parent component has to pass all those data down the road as props . 因为父组件必须将所有这些数据作为props传递下去。 When a child component's data requirements need change, it has a cascading effect of change up all the way to the parent. 当子组件的数据要求需要更改时,它具有一直到父级的更改的级联效果。

Relay solves this problem by making it possible for the child components to modify their own data requirements independently, without having to modify the chain up in the component tree. 中继通过使子组件可以独立地修改自己的数据要求来解决此问题,而无需在组件树中修改链。 The parent component just needs to declare which child components' data requirements it covers. 父组件只需要声明它涵盖哪些子组件的数据要求。 So, the parent does not need to know the details of components. 因此,父母不需要知道组件的细节。

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

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