简体   繁体   English

将多个数组传递到同一组件的React / Redux最佳实践

[英]React/Redux Best practices on passing multiple arrays to same component

I'm getting some data from a third-party API. 我从第三方API获取一些数据。 I must do some operations in one of the values we receive (price). 我必须对我们收到的值之一(价格)进行一些操作。 The modifications are being done successfully at the Action creator and stored in a new array called price, and being passed as props. 修改已在Action创建者处成功完成,并存储在名为price的新数组中,并作为道具传递。

The individual Product component is being rendered after using the map method on the array of products. 在产品数组上使用map方法后,将渲染单个Product组件。

My question is, how do I pass the new array (modified price) down to this Product component (and apply it for the correct product). 我的问题是,如何将新数组(修改后的价格)传递给该产品组件(并将其应用于正确的产品)。

const CoinList = (props) => {
 return(
    <View>
            {
                props.productList.map(product => {
                    return <Product key={product.id} {...product}/>
                })
            }

    </View>
 )
};

As the new price is being added as 'price' prop. 随着新价格被添加为“价格”道具。 I naturally tried <Product price={props.price} key={product.index} {...product} /> which ends up displaying the entire array of prices on each product. 我自然尝试了<Product price={props.price} key={product.index} {...product} /> ,最终显示了每个产品的全部价格。

I also tried converting this array into an array of objects, and spreading it <Product key={product.index} {...product} {...props.price}/> but this doesn't seem to work either. 我还尝试将该数组转换为对象数组,然后将其传播<Product key={product.index} {...product} {...props.price}/>但这似乎也不起作用。

What would be your best approach to this type of situation? 对于这种情况,您最好的方法是什么?

I would do this logic in a selector and use reselect to memoize it: https://github.com/reactjs/reselect . 我将在选择器中执行此逻辑,然后使用reselect对其进行记忆: https : //github.com/reactjs/reselect

If you aren't already using selectors that is a pretty well acknowledged best practice for handling derived data: https://gist.github.com/abhiaiyer91/aaf6e325cf7fc5fd5ebc70192a1fa170 如果您尚未使用选择器,那是公认的处理派生数据的最佳做法: https : //gist.github.com/abhiaiyer91/aaf6e325cf7fc5fd5ebc70192a1fa170

If productList and priceList arrays have the same length you can use index to get matching price from priceList array for current product and pass it as prop. 如果productList和priceList数组的长度相同,则可以使用index从priceList数组中获取当前产品的匹配价格,并将其作为prop传递。

 class Product extends React.Component { render() { let {id, name, price} = this.props; return <div className="Product">{name} | {price} $</div> } } const CoinList = (props) => { return <div>{props.productList.map((product, i) => { return <Product price={props.priceList[i]} key={product.id} {...product}/>}) }</div> }; const products = [{id: 1, name: 'A'}, {id: 2, name: 'B'}] const prices = [100, 200] ReactDOM.render( <CoinList productList={products} priceList={prices} />, document.getElementById('app') ) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

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

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