简体   繁体   English

如何拆分大型React组件?

[英]How do I split up a large react component?

I'm learning react by programming the cardgame Solitaire. 我正在通过编程纸牌接龙来学习反应。 My main component, where I render the game board and the cards, has become very large (700 lines and growing). 我用来渲染游戏板和卡片的主要组件已经变得非常大(700条线并且还在不断增加)。

The big bulk of that is all animation related and I would like to throw all the animation related methods into different files. 其中大部分是与动画相关的,我想将所有与动画相关的方法放入不同的文件中。

I'm not sure how to go about that in React. 我不确定如何在React中解决这个问题。 If I was doing this in Ruby, I would simply create a helper module, import the module and call the functions on the module. 如果我在Ruby中进行此操作,则只需创建一个帮助程序模块,导入该模块并调用该模块上的函数。 Creating a helper file and putting the functions in there doesn't seem to work in React, because I don't have access to "this" in those functions. 在React中似乎无法创建一个辅助文件并将函数放置在其中,因为我无法在那些函数中访问“ this”。

How would you deal with this situation in react? 您将如何应对这种情况? What patterns do people usually use when they want to separate out the logic in React components? 人们想要分离React组件中的逻辑时通常使用什么模式?

Edit 1 (added example component): 编辑1(添加示例组件):

I've already read the React docs and I am already using Redux. 我已经阅读了React文档,并且已经在使用Redux。 My issue is that i have a lot of animations and UI moving around using JavaScript. 我的问题是,我有很多动画和UI使用JavaScript移动。 Here's a link to the component I'm talking about: 这是我正在谈论的组件的链接:

https://gist.github.com/holgersindbaek/b8f134306148d12995dadf5e28d2d5b7 https://gist.github.com/holgersindbaek/b8f134306148d12995dadf5e28d2d5b7

I'm keeping the current state of the game in the components "state". 我将游戏的当前状态保留在组件“状态”中。 Once the Redux state changes, the props update and an animation is set off (move a card from old position "state" to new position "props". Once the animation is complete, the state of the component is updated so it's the same as the props. Redux状态更改后,道具会更新并设置动画(将卡片从旧位置“ state”移动到新位置“ props”。动画完成后,组件的状态也会更新,因此与道具。

My issue is that there's a lot of non-essential code in that component that's essentially just moving things around on the screen (initializeNewGame, initializeInteract, positionCardsOnBoard). 我的问题是,该组件中有很多不必要的代码,它们实际上只是在屏幕上移动内容(initializeNewGame,initializeInteract,positionCardsOnBoard)。 How would you deal with such methods? 您将如何处理这些方法?

Edit 2 (I think I figured it out): 编辑2(我想我知道了):

I've decided to place some of the animation functions in a helper file (animationHelper.js) and then import it in the React component and bind it to the component, so I can reference "this" in the animation function. 我决定将一些动画功能放在帮助程序文件(animationHelper.js)中,然后将其导入React组件并将其绑定到该组件,因此我可以在动画函数中引用“ this”。 I've updated the example to reflect the new changes: 我已经更新了示例以反映新的变化:

https://gist.github.com/holgersindbaek/b8f134306148d12995dadf5e28d2d5b7 https://gist.github.com/holgersindbaek/b8f134306148d12995dadf5e28d2d5b7

That seems like a good solution to me. 对我来说,这似乎是一个很好的解决方案。 Am I breaking any best practices by doing this? 我这样做是否违反了最佳做法?

I think you need something like Redux . 我认为您需要Redux之类的东西。 Redux can help you keep your code scalable and maintainable. Redux可以帮助您保持代码的可伸缩性和可维护性。 Basicaly, Redux allows you to split each component in two parts (or files), one for the view (components), and another for the busines logic (containers), which helps you test your code easely. 基本上,Redux允许您将每个组件分为两部分(或文件),一个部分用于视图(组件),另一部分用于业务逻辑(容器),这可以帮助您轻松地测试代码。 Redux is a design pattern of unidirectional data flow. Redux是单向数据流的设计模式。

In adition, you can apply some SOLID principles , applying Single Responsability for each component. 此外,您可以应用一些SOLID原则 ,对每个组件应用单一职责。

You can check an real example here . 您可以在此处查看一个真实的示例。

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

相关问题 如何在 Express 中将大型控制器拆分为多个文件? - How do you split up large controllers into multiple files in Express? 如何在反应中将单个组件代码拆分为多个文件 - How to split up single component code into multiple files in react 如何获得浏览器的自动填充值以显示在序列化的React`Field`组件数据中? - How do I get browser Autofill values to show up in my serialized React `Field` component data? 我如何使用 jsdoc 注释注释 React.forwardRef 以便智能感知可以显示在底层组件中? - How do i annotate React.forwardRef with jsdoc comments so that intellisense can show up for the underlying component? 如何将大型数据集拆分为 2 以使用 Tensorflow.js 进行验证? - How do I split a large Dataset in 2 for validation with Tensorflow.js? 我如何在React-native中异步呈现大型组件 - How can I asynchronously render a large Component in react-native 如何在React中拆分导航栏? - How should I split up a nav bar in React? 如何在反应组件中编写 If 语句? - How do I write a an If statement in a react component? 如何在 React Modal 上显示组件 - How do I display a component on React Modal 如何在组件 function 上使用“this”进行反应? - how do I use "this" on an component function in react?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM