我正在使用web3,它包括从智能合约和其他库获取数据的连接。最后我创建了两个切片
- 1个智能合约切片,从智能合约(如循环供应(中获取数据
- 1个价格数据切片
然后在应用程序的许多页面中,我使用一个称为";MarketCap";这是价格数据之间的计算:
marketCap = circulatingSupply * price
在根组件中,我这样做:
...
useEffect(() => {
dispatch(initializeProviderContracts({ networkID: chainID, provider }));
dispatch(getBlockchainData(provider));
dispatch(getMarketPrices());
}, [connected]);
...
我为每个组件做的是:
Comp
const component = () => {
// get the states
const price = useSelector(state => state.market.price);
const circulatingSupply = useSelector(state => state.contracts.token.circulatingSupply);
const marketCap = price * circulatingSupply; // Done several times
}
我在Redux中找不到任何关于这方面的提示,最好的方法是什么?我需要创建另一个像metrics
一样的切片并添加一个额外的调度器吗?
useEffect(() => {
dispatch(initializeProviderContracts({ networkID: chainID, provider }));
dispatch(getBlockchainData(provider));
dispatch(getMarketPrices());
dispatch(computeMetrics()); // could be used to compute marketCap here
}, [connected]);
还有这些类型的redux操作的名称吗?
谢谢!
对于派生值,请使用选择器。
// somewhere
export const selectMarketCap = state => state.market.price * state.contracts.token.circulatingSupply;
// in your component
const marketCap = useSelector(selectMarketCap);