Im using the Coincap web hook, this webhook returns data like this:
{ ‘bitcoin’: 12322, ‘ethereum’: 12322 }
I have this react native code, Why when i call the useUpdateCoinAssetPriceAction with the params {id: ‘bitcoin’, data: {price: 11, timestamp: new Date().getTime()}} the ItemA re-render but also the ItemB?
type CoinAsset = {
readonly id: string;
readonly price: number;
readonly timestamp: number;
};
export type CoinAssetPrice = {
readonly price: number;
readonly timestamp: number;
};
export type CoinAssetPrices = {
[key: string]: CoinAssetPrice;
};
export interface CoinAssetsState {
prices: CoinAssetPrices;
}
export const initialState: CoinAssetsState = {
prices: {},
};
const slice = createSlice({
name: "/coin_assets",
initialState,
reducers: {
updateCoinAssetPrice: (
state,
{
payload: { id, data },
}: PayloadAction<{ readonly id: string; readonly data: CoinAssetPrice }>
) => {
state.prices[id] = data;
},
},
});
export const { updateCoinAssetPrice } = slice.actions;
export type AppSelector<Response, Request = null> = Selector<
AppState,
Response,
Request
>;
const assetSelector: AppSelector<CoinAsset, CoinAsset> = (_, coinAsset) =>
coinAsset;
const prices: AppSelector<CoinAssetPrices> = ({ coinAssetsReducer }) =>
coinAssetsReducer.prices;
const priceSelector = createSelector(
prices,
assetSelector,
(data, { id, price, timestamp }) => {
return data[id] || { price, timestamp };
}
);
export const useUpdateCoinAssetPriceAction = () => {
const dispatch = useAppDispatch();
return useCallback(
(prices: { readonly id: string; readonly data: CoinAssetPrice }) => {
dispatch(updateCoinAssetPrice(prices));
},
[dispatch]
);
};
export const useCoinAssetPrice = (coinAsset: CoinAsset) =>
useAppSelector((appState) => priceSelector(appState, coinAsset));
const ItemA: React.FC = () => {
const price = useCoinAssetPrice({id: 'bitcoin', price: 1200, timestamp: new Date().getTime()})
return (<Text>{price}</Text>)
}
const ItemB: React.FC = () => {
const price = useCoinAssetPrice({
id: "ethereum",
price: 1200,
timestamp: new Date().getTime(),
});
return (<Text>{price}</Text>)
};
I dont want to re-render the ‘bitcoin’ item if the webhook only return ‘ethereum’ data.