Consuming ReactContext (specifically) Consumers across multiple packages












0














Context
I created a Context and exported both Producer and Consumer. I am now wrapping my app level example with Producer. I want to be able to consume ContextConsumer inside a module which is served by a different package.



However, when I try to use ContextConsumer inside such module, it throws out the error, cannot call function of undefined.



Code Structure
The below is how I have my code structured.



Context-v1 package



Context.js



import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import ZIndexUtils from './zIndexUtils';

const { node } = PropTypes;
const { Provider, Consumer: IDSContextConsumer } = createContext();

class IDSContextProvider extends Component {
static propTypes = {
children: node.isRequired
};

state = {
topZIndex: ZIndexUtils.getTopZIndex(),
incrementTopZIndex: ZIndexUtils.incrementTopZIndex
};

render() {
return (
<Provider
value={{
topZIndex: this.state.topZIndex,
incrementTopZIndex: this.state.incrementTopZIndex
}}
>
{this.props.children}
</Provider>
);
}
}

export { IDSContextProvider };

export default IDSContextConsumer;


index.js



import IDSContextConsumer, { IDSContextProvider } from './Context';

export {
IDSContextProvider,
IDSContextConsumer
};


Dropdown-v1 package
This component makes use of another component called as Menu-v1 which is where I am trying to use Consumer to access the increment function that I am passing down from app level example.



import { IDSContextConsumer } from '@ids/context-v1';
...
...
render() {
return (
<IDSContextConsumer>
{
(context) => (
<ZIndex assignZIndex getTopZindex={context.incrementTopZIndex}>
<MenuList
className={className}
autoFocus={autoFocus}
aria-label={this.props['aria-label']}
onKeyDown={this.handleKeyDown}
onBlur={this.handleMenuBlur}
reference={reference}
ref={refReactDom}
style={style}
>
{items}
</MenuList>
</ZIndex>
)
}
</IDSContextConsumer>
)
}


App example
Finally, I am trying to use this Dropdown module into my app. Which is where I expect to see the incrementer function be passed in via context.



import { IDSContextProvider } from '@ids/context-v1';
import { Dropdown, MenuItem } from '@ids/dropdown';



render() {
return (
<div>
<IDSContextProvider>
<Modal
open={this.state.openModalDialog}
onCloseModalDialog={this.handleModalClosing}
title="Modal with Dropdown"
>
<Dropdown
onBlur={() => console.log('Dropdown blurrrrr')}
label="Name"
value={this.state.value}
onChange={this.handleDropdownChange}
>
<MenuItem value="1">Banana</MenuItem>
<MenuItem value="2">Apple</MenuItem>
<MenuItem value="3">Guava</MenuItem>
<MenuItem value="4">Mango</MenuItem>
</Dropdown>
</Modal>
</IDSContextProvider>
<button className="divRenderButton" onClick={this.handleModalOpening}>Open Modal</button>
</div>
);
}


Modal component in my example



<Portal>
<Transition
enterClassName={`${prefix}--slideIn`}
transition={open ? transitions.enter : transitions.exit}
onEntered={onShow}
onExited={onClose}
exitClassName={`${prefix}--slideOut`}
unmountOnExit
>
<IDSContextConsumer>
{
(context) => (
<ZIndex
assignZIndex={open}
underlay
getTopZindex={context.incrementTopZIndex}
>
<div
className={open ? 'divContainerWithUnderlay' : ''}
>
{
open &&
<div>
<h1 className="divContent">{title}</h1>
{
this.props.children ? this.props.children : null
}
<button className="divRenderButton" onClick={this.closeModalDialog}>Close Modal</button>
</div>
}
</div>
</ZIndex>
)
}
</IDSContextConsumer>
</Transition>
</Portal>


Requesting earnest help on this. I have tried the solutions from this forum before and cannot find any approach where the modules from different packages share the context.










share|improve this question






















  • cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
    – estus
    Nov 21 at 7:49
















0














Context
I created a Context and exported both Producer and Consumer. I am now wrapping my app level example with Producer. I want to be able to consume ContextConsumer inside a module which is served by a different package.



However, when I try to use ContextConsumer inside such module, it throws out the error, cannot call function of undefined.



Code Structure
The below is how I have my code structured.



Context-v1 package



Context.js



import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import ZIndexUtils from './zIndexUtils';

const { node } = PropTypes;
const { Provider, Consumer: IDSContextConsumer } = createContext();

class IDSContextProvider extends Component {
static propTypes = {
children: node.isRequired
};

state = {
topZIndex: ZIndexUtils.getTopZIndex(),
incrementTopZIndex: ZIndexUtils.incrementTopZIndex
};

render() {
return (
<Provider
value={{
topZIndex: this.state.topZIndex,
incrementTopZIndex: this.state.incrementTopZIndex
}}
>
{this.props.children}
</Provider>
);
}
}

export { IDSContextProvider };

export default IDSContextConsumer;


index.js



import IDSContextConsumer, { IDSContextProvider } from './Context';

export {
IDSContextProvider,
IDSContextConsumer
};


Dropdown-v1 package
This component makes use of another component called as Menu-v1 which is where I am trying to use Consumer to access the increment function that I am passing down from app level example.



import { IDSContextConsumer } from '@ids/context-v1';
...
...
render() {
return (
<IDSContextConsumer>
{
(context) => (
<ZIndex assignZIndex getTopZindex={context.incrementTopZIndex}>
<MenuList
className={className}
autoFocus={autoFocus}
aria-label={this.props['aria-label']}
onKeyDown={this.handleKeyDown}
onBlur={this.handleMenuBlur}
reference={reference}
ref={refReactDom}
style={style}
>
{items}
</MenuList>
</ZIndex>
)
}
</IDSContextConsumer>
)
}


App example
Finally, I am trying to use this Dropdown module into my app. Which is where I expect to see the incrementer function be passed in via context.



import { IDSContextProvider } from '@ids/context-v1';
import { Dropdown, MenuItem } from '@ids/dropdown';



render() {
return (
<div>
<IDSContextProvider>
<Modal
open={this.state.openModalDialog}
onCloseModalDialog={this.handleModalClosing}
title="Modal with Dropdown"
>
<Dropdown
onBlur={() => console.log('Dropdown blurrrrr')}
label="Name"
value={this.state.value}
onChange={this.handleDropdownChange}
>
<MenuItem value="1">Banana</MenuItem>
<MenuItem value="2">Apple</MenuItem>
<MenuItem value="3">Guava</MenuItem>
<MenuItem value="4">Mango</MenuItem>
</Dropdown>
</Modal>
</IDSContextProvider>
<button className="divRenderButton" onClick={this.handleModalOpening}>Open Modal</button>
</div>
);
}


Modal component in my example



<Portal>
<Transition
enterClassName={`${prefix}--slideIn`}
transition={open ? transitions.enter : transitions.exit}
onEntered={onShow}
onExited={onClose}
exitClassName={`${prefix}--slideOut`}
unmountOnExit
>
<IDSContextConsumer>
{
(context) => (
<ZIndex
assignZIndex={open}
underlay
getTopZindex={context.incrementTopZIndex}
>
<div
className={open ? 'divContainerWithUnderlay' : ''}
>
{
open &&
<div>
<h1 className="divContent">{title}</h1>
{
this.props.children ? this.props.children : null
}
<button className="divRenderButton" onClick={this.closeModalDialog}>Close Modal</button>
</div>
}
</div>
</ZIndex>
)
}
</IDSContextConsumer>
</Transition>
</Portal>


Requesting earnest help on this. I have tried the solutions from this forum before and cannot find any approach where the modules from different packages share the context.










share|improve this question






















  • cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
    – estus
    Nov 21 at 7:49














0












0








0







Context
I created a Context and exported both Producer and Consumer. I am now wrapping my app level example with Producer. I want to be able to consume ContextConsumer inside a module which is served by a different package.



However, when I try to use ContextConsumer inside such module, it throws out the error, cannot call function of undefined.



Code Structure
The below is how I have my code structured.



Context-v1 package



Context.js



import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import ZIndexUtils from './zIndexUtils';

const { node } = PropTypes;
const { Provider, Consumer: IDSContextConsumer } = createContext();

class IDSContextProvider extends Component {
static propTypes = {
children: node.isRequired
};

state = {
topZIndex: ZIndexUtils.getTopZIndex(),
incrementTopZIndex: ZIndexUtils.incrementTopZIndex
};

render() {
return (
<Provider
value={{
topZIndex: this.state.topZIndex,
incrementTopZIndex: this.state.incrementTopZIndex
}}
>
{this.props.children}
</Provider>
);
}
}

export { IDSContextProvider };

export default IDSContextConsumer;


index.js



import IDSContextConsumer, { IDSContextProvider } from './Context';

export {
IDSContextProvider,
IDSContextConsumer
};


Dropdown-v1 package
This component makes use of another component called as Menu-v1 which is where I am trying to use Consumer to access the increment function that I am passing down from app level example.



import { IDSContextConsumer } from '@ids/context-v1';
...
...
render() {
return (
<IDSContextConsumer>
{
(context) => (
<ZIndex assignZIndex getTopZindex={context.incrementTopZIndex}>
<MenuList
className={className}
autoFocus={autoFocus}
aria-label={this.props['aria-label']}
onKeyDown={this.handleKeyDown}
onBlur={this.handleMenuBlur}
reference={reference}
ref={refReactDom}
style={style}
>
{items}
</MenuList>
</ZIndex>
)
}
</IDSContextConsumer>
)
}


App example
Finally, I am trying to use this Dropdown module into my app. Which is where I expect to see the incrementer function be passed in via context.



import { IDSContextProvider } from '@ids/context-v1';
import { Dropdown, MenuItem } from '@ids/dropdown';



render() {
return (
<div>
<IDSContextProvider>
<Modal
open={this.state.openModalDialog}
onCloseModalDialog={this.handleModalClosing}
title="Modal with Dropdown"
>
<Dropdown
onBlur={() => console.log('Dropdown blurrrrr')}
label="Name"
value={this.state.value}
onChange={this.handleDropdownChange}
>
<MenuItem value="1">Banana</MenuItem>
<MenuItem value="2">Apple</MenuItem>
<MenuItem value="3">Guava</MenuItem>
<MenuItem value="4">Mango</MenuItem>
</Dropdown>
</Modal>
</IDSContextProvider>
<button className="divRenderButton" onClick={this.handleModalOpening}>Open Modal</button>
</div>
);
}


Modal component in my example



<Portal>
<Transition
enterClassName={`${prefix}--slideIn`}
transition={open ? transitions.enter : transitions.exit}
onEntered={onShow}
onExited={onClose}
exitClassName={`${prefix}--slideOut`}
unmountOnExit
>
<IDSContextConsumer>
{
(context) => (
<ZIndex
assignZIndex={open}
underlay
getTopZindex={context.incrementTopZIndex}
>
<div
className={open ? 'divContainerWithUnderlay' : ''}
>
{
open &&
<div>
<h1 className="divContent">{title}</h1>
{
this.props.children ? this.props.children : null
}
<button className="divRenderButton" onClick={this.closeModalDialog}>Close Modal</button>
</div>
}
</div>
</ZIndex>
)
}
</IDSContextConsumer>
</Transition>
</Portal>


Requesting earnest help on this. I have tried the solutions from this forum before and cannot find any approach where the modules from different packages share the context.










share|improve this question













Context
I created a Context and exported both Producer and Consumer. I am now wrapping my app level example with Producer. I want to be able to consume ContextConsumer inside a module which is served by a different package.



However, when I try to use ContextConsumer inside such module, it throws out the error, cannot call function of undefined.



Code Structure
The below is how I have my code structured.



Context-v1 package



Context.js



import React, { Component, createContext } from 'react';
import PropTypes from 'prop-types';
import ZIndexUtils from './zIndexUtils';

const { node } = PropTypes;
const { Provider, Consumer: IDSContextConsumer } = createContext();

class IDSContextProvider extends Component {
static propTypes = {
children: node.isRequired
};

state = {
topZIndex: ZIndexUtils.getTopZIndex(),
incrementTopZIndex: ZIndexUtils.incrementTopZIndex
};

render() {
return (
<Provider
value={{
topZIndex: this.state.topZIndex,
incrementTopZIndex: this.state.incrementTopZIndex
}}
>
{this.props.children}
</Provider>
);
}
}

export { IDSContextProvider };

export default IDSContextConsumer;


index.js



import IDSContextConsumer, { IDSContextProvider } from './Context';

export {
IDSContextProvider,
IDSContextConsumer
};


Dropdown-v1 package
This component makes use of another component called as Menu-v1 which is where I am trying to use Consumer to access the increment function that I am passing down from app level example.



import { IDSContextConsumer } from '@ids/context-v1';
...
...
render() {
return (
<IDSContextConsumer>
{
(context) => (
<ZIndex assignZIndex getTopZindex={context.incrementTopZIndex}>
<MenuList
className={className}
autoFocus={autoFocus}
aria-label={this.props['aria-label']}
onKeyDown={this.handleKeyDown}
onBlur={this.handleMenuBlur}
reference={reference}
ref={refReactDom}
style={style}
>
{items}
</MenuList>
</ZIndex>
)
}
</IDSContextConsumer>
)
}


App example
Finally, I am trying to use this Dropdown module into my app. Which is where I expect to see the incrementer function be passed in via context.



import { IDSContextProvider } from '@ids/context-v1';
import { Dropdown, MenuItem } from '@ids/dropdown';



render() {
return (
<div>
<IDSContextProvider>
<Modal
open={this.state.openModalDialog}
onCloseModalDialog={this.handleModalClosing}
title="Modal with Dropdown"
>
<Dropdown
onBlur={() => console.log('Dropdown blurrrrr')}
label="Name"
value={this.state.value}
onChange={this.handleDropdownChange}
>
<MenuItem value="1">Banana</MenuItem>
<MenuItem value="2">Apple</MenuItem>
<MenuItem value="3">Guava</MenuItem>
<MenuItem value="4">Mango</MenuItem>
</Dropdown>
</Modal>
</IDSContextProvider>
<button className="divRenderButton" onClick={this.handleModalOpening}>Open Modal</button>
</div>
);
}


Modal component in my example



<Portal>
<Transition
enterClassName={`${prefix}--slideIn`}
transition={open ? transitions.enter : transitions.exit}
onEntered={onShow}
onExited={onClose}
exitClassName={`${prefix}--slideOut`}
unmountOnExit
>
<IDSContextConsumer>
{
(context) => (
<ZIndex
assignZIndex={open}
underlay
getTopZindex={context.incrementTopZIndex}
>
<div
className={open ? 'divContainerWithUnderlay' : ''}
>
{
open &&
<div>
<h1 className="divContent">{title}</h1>
{
this.props.children ? this.props.children : null
}
<button className="divRenderButton" onClick={this.closeModalDialog}>Close Modal</button>
</div>
}
</div>
</ZIndex>
)
}
</IDSContextConsumer>
</Transition>
</Portal>


Requesting earnest help on this. I have tried the solutions from this forum before and cannot find any approach where the modules from different packages share the context.







reactjs react-context react-16






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 23:08









Krishnamachari Shrikanth

1




1












  • cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
    – estus
    Nov 21 at 7:49


















  • cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
    – estus
    Nov 21 at 7:49
















cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
– estus
Nov 21 at 7:49




cannot call function of undefined - please, provide exact error message, I believe you misinterpreted it. Please, provide stackoverflow.com/help/mcve , it's required for SO questions. The question contains a lot of irrelevant code that could possibly be skipped. A working demo like Stackblitz or Codesandbox will increase chances to solve the problem. The situation with multiple packages is irrelevant. It doesn't matter how many packages are there, as long as exports and imports are correct. It's the same for local modules.
– estus
Nov 21 at 7:49

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402966%2fconsuming-reactcontext-specifically-consumers-across-multiple-packages%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402966%2fconsuming-reactcontext-specifically-consumers-across-multiple-packages%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python