recompose icon indicating copy to clipboard operation
recompose copied to clipboard

setStatic function

Open webbygregg opened this issue 8 years ago • 3 comments

I am trying to set a static function called fetch to be used with react-router-fetch but I can't seem to get it to work and cant find any examples about it.

I presume I use setStatic and have tried this:

const Testing = setStatic(
  'fetch',
  () => new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, { test: '1234' });
  })
)(Content);

Any ideas if this is possible?

webbygregg avatar Jan 29 '18 11:01 webbygregg

I got it working as above but there seems to be an issue with calling setStatic after withProps in compose

webbygregg avatar Jan 29 '18 12:01 webbygregg

If you want the consumer of your component can access your static properties, you should wrap your components with setStatic in the last step, for example:

Do

compose(
  setStatic(...),
  withProps(...)
)(Content)

Instead of

compose(
  withProps(...),
  setStatic(...)
)(Content)

It's because withProps create and return a new component which doesn't have the setted static properties.

wuct avatar Feb 24 '18 03:02 wuct

BTW if you want to hoist your static properties from the inner component to the outer component, you can use hoistStatics.

wuct avatar Feb 24 '18 03:02 wuct