react-context-consumer
Small and lightweight context consumer for your class-components
Overview
With new Context API to consume several contexts we need to put several context consumers in our component (one for each context):
; { return <ThemeContextConsumer> <LocaleContextConsumer> <StorageContextConsumer> // render component using theme, locale and storage </StorageContextConsumer> </LocaleContextConsumer> </ThemeContextConsumer> ; }
react-context-consumer
helps to simplify render()
function in such situations. Just pass several contexts into contexts
prop and consume all of them at once:
;; { return <ReactContextConsumer contexts= ThemeContext LocaleContext StorageContext > { // render component using theme, locale and storage } </ReactContextConsumer> ; }
I guess, it looks nicer =)
Additional arguments for renderProp function
Let's imagine we already have a component using react context consumers and custom render prop with additional arguments like this one:
Component ... { return <ThemeContextConsumer> <LocaleContextConsumer> this </LocaleContextConsumer> </ThemeContextConsumer> } { ... }
You can now just pass all needed additional arguments using args
prop:
Component ... { return <ContextConsumer contexts= ThemeContext LocaleContext args= someValue > thisrenderComponentWithContext </ContextConsumer> } ...