简体   繁体   English

组件的React.js道具

[英]React.js props for component

Basing on this example https://jsfiddle.net/reactjs/n47gckhr/light/ taken from React's official docs https://facebook.github.io/react/docs/thinking-in-react.html 基于此示例https://jsfiddle.net/reactjs/n47gckhr/light/取自React的官方文档https://facebook.github.io/react/docs/thinking-in-react.html

1) If I were to create multiple FilterableProductTable and having to get the data from server, I'll have to place the ajax method in a parent component and pass the data to FilterableProductTable (as in the example) on initialisation. 1)如果要创建多个FilterableProductTable并必须从服务器获取数据,则必须在初始化时将ajax方法放在父组件中,并将数据传递给FilterableProductTable (如示例中所示)。

2) Wouldn't it make sense to have the ajax method inside FilterableProductTable to fetch the data it needs(and be an independent component). 2)在FilterableProductTable使用ajax方法来获取所需的数据(并是一个独立的组件)是否有意义。 But if do that I will have to make the data(products in the example) a state of FilterableProductTable and not props since props can't be changed by its own component. 但是如果这样做,我将必须使数据(示例中的产品)处于FilterableProductTable的状态,而不是props的状态,因为props不能由其自己的组件更改。 But making the products as state doesn't sound right. 但是,将产品定为状态听起来并不正确。

So is option 1 better or should I go with option 2? 那么选项1更好还是应该选择选项2?

Option 1 is better than option 2 but using a flux pattern would be better still. 选项1优于选项2,但使用通量模式仍会更好。 The primary advantage of using a flux pattern is better separation of concerns. 使用磁通量模式的主要优点是更好的关注点分离。 It is good to have your data logic separate from your display logic. 最好将数据逻辑与显示逻辑分开。 This results in easier maintenance of your project. 这样可以简化项目的维护。

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

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