简体   繁体   English

如何在通过NPM分发时管理React组件(Redux,CSS)的自定义方面

[英]How to manage custom aspects of a React component (Redux, CSS) whilst distributing via NPM

Question: How are custom Redux containers and CSS normally handled through NPM? 问题:如何通过NPM正常处理自定义Redux容器和CSS?
The below structure doesn't work well with traditional package distribution platforms such as NPM as I need to edit the custom files such as the Redux container and the CSS in different projects. 以下结构不适用于传统的包分发平台,如NPM,因为我需要在不同的项目中编辑自定义文件,如Redux容器和CSS。


Given that I have many components in a custom React component library with the following structure and files: 鉴于我在具有以下结构和文件的自定义React组件库中有许多组件:

> Component root dir
 > Component.js
 > Component.css 
 > Component_container.js
 > Component_custom.css
  • The Redux container holds store references which change from project to project Redux容器包含从项目更改为项目的商店引用
  • The CSS also holds customisable styling for each project CSS还为每个项目保留可定制的样式
  • These components reference other components through their containers 这些组件通过其容器引用其他组件

For example: 例如:

App.js   
  => Component_1_container.js   
    => Component_1.js   
      => Component_2_container.js
        => Component_2.js 


This is great in that it allows me to separate out custom code from the shared code, I can update Component.js and Component.css in many projects without touching the code in the custom files. 这很棒,因为它允许我从共享代码中分离出自定义代码,我可以在许多项目中更新Component.js和Component.css,而无需触及自定义文件中的代码。 However the custom container and CSS files can't be managed through NPM. 但是,无法通过NPM管理自定义容器和CSS文件。

I can easily see how the CSS could be extracted into a separate folder. 我可以很容易地看到CSS如何被提取到一个单独的文件夹中。 The Redux containers are harder because they are referenced by other components as dependencies as in the above structure. Redux容器更难,因为它们被其他组件引用为上述结构中的依赖项。 Moving them out of NPM and into another project folder would make references between components difficult to manage. 将它们从NPM移到另一个项目文件夹中会使组件之间的引用难以管理。

I was thinking of very similar issue and I think you could create a separate module (no matter npm module or just a folder in you're project marked as a module) named ui-kit and all components there will be Redux free, since the client of this components can use other store or work with out any. 我正在考虑非常类似的问题,我认为你可以创建一个单独的模块(无论npm模块或只是你的项目标记为模块的文件夹)名为ui-kit ,所有组件都将是Redux免费的,因为这个组件的客户端可以使用其他商店或任何工作。

Also, you're gonna have folder component or something you have now and your high-level component (App.js) will know only about your component which will know about ui-kit 此外,你将拥有文件夹component或你现在拥有的东西,你的高级组件 (App.js)将只知道你的组件 ,它将了解ui-kit

Structure: 结构体:

app
  -> Components (containers)
    -> Component.js
    -> Component.css
  -> ui-kit (Redux free components)
    -> Component.js
    -> Component.css
  App.js (High-level component)

Having been through quite a guides and example repos, it seems that the best approach to this is to have just a few container elements sitting at a higher level controlling many related components. 通过相当的指南和示例回购,似乎最好的方法是只有几个容器元素坐在更高级别控制许多相关组件。

I felt it was a decent idea for each component to have individual access to the store with its own container and which could change from project to project, but it is an unmanageable solution. 我觉得每个组件都可以通过自己的容器单独访问商店,并且可以在不同项目之间进行更改,但这是一个难以管理的解决方案。

The main takeaway here is that presentational components should never call container components as a dependency. 这里的主要内容是,表示组件不应该将容器组件称为依赖项。

Having separated out container and custom style files from the component directory, it is straightforward enough to then create NPM components which contain only the reusable code. 将容器和自定义样式文件从组件目录中分离出来之后,就可以直接创建仅包含可重用代码的NPM组件。

The question is how many containers to use in an app, here is a good summary of how to grouped components against containers which I found useful in answering this question: http://www.thegreatcodeadventure.com/the-react-plus-redux-container-pattern/ 问题是在应用程序中使用了多少个容器,这里是如何根据容器对组件进行分组的一个很好的总结,我发现在回答这个问题时很有用: http//www.thegreatcodeadventure.com/the-react-plus-redux -container图案/

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

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