drizzle icon indicating copy to clipboard operation
drizzle copied to clipboard

How to use multi call approach without react hooks?

Open cayblood opened this issue 5 years ago • 1 comments
trafficstars

On the react-plugin readme, there is a multi call example with hooks. I'm wondering how to do this with cacheCall. All the cacheCall examples only call a single contract method. What if you need to take the result of one call and use the response as parameters for another call? Here is the example:

import React from 'react';
import { drizzleReactHooks } from ''@drizzle/react-plugin';
import Balance from './components/balance';

export default () => {
  const { useCacheCall } = drizzleReactHooks.useDrizzle();
  const drizzleState = drizzleReactHooks.useDrizzleState(drizzleState => ({
    accounts: drizzleState.accounts
  }))
  return (
    <Balance
      balance={useCacheCall(['MyToken'], call =>
        drizzleState.accounts.reduce(
          (sum, account) => sum + (call('MyToken', 'balanceOf', account) || 0),
          0
        )
      )}
    />
  );
}

How would you do this without react hooks, using the regular cacheCall approach?

cayblood avatar Nov 25 '19 03:11 cayblood

@cayblood In the hook, you can see that hey basically call cacheCall multiple times internally: https://github.com/trufflesuite/drizzle/blob/develop/packages/react-plugin/src/hooks/create-use-cache-call.js#L33

At present, there's nothing built-in to Drizzle that does this for you. So in effect, you'd need to re-write the logic from create-use-cache-call.js where isFunction is true.

adrianmcli avatar Nov 25 '19 08:11 adrianmcli