React multiple contexts











up vote
0
down vote

favorite












I am using functions which are passed down through context.



ChildComponent.contextType = SomeContext;


Now I use this.context.someFunction();. This works.



How can I do this if I need functions from two different parent components?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am using functions which are passed down through context.



    ChildComponent.contextType = SomeContext;


    Now I use this.context.someFunction();. This works.



    How can I do this if I need functions from two different parent components?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am using functions which are passed down through context.



      ChildComponent.contextType = SomeContext;


      Now I use this.context.someFunction();. This works.



      How can I do this if I need functions from two different parent components?










      share|improve this question













      I am using functions which are passed down through context.



      ChildComponent.contextType = SomeContext;


      Now I use this.context.someFunction();. This works.



      How can I do this if I need functions from two different parent components?







      reactjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 at 22:56









      ATOzTOA

      19.2k166698




      19.2k166698
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can still use function-as-a-child consumer nodes with the 16.3 Context API, which is what the React documentation suggests doing:



          // Theme context, default to light theme
          const ThemeContext = React.createContext('light');

          // Signed-in user context
          const UserContext = React.createContext({
          name: 'Guest',
          });

          class App extends React.Component {
          render() {
          const {signedInUser, theme} = this.props;

          // App component that provides initial context values
          return (
          <ThemeContext.Provider value={theme}>
          <UserContext.Provider value={signedInUser}>
          <Layout />
          </UserContext.Provider>
          </ThemeContext.Provider>
          );
          }
          }

          function Layout() {
          return (
          <div>
          <Sidebar />
          <Content />
          </div>
          );
          }

          // A component may consume multiple contexts
          function Content() {
          return (
          <ThemeContext.Consumer>
          {theme => (
          <UserContext.Consumer>
          {user => (
          <ProfilePage user={user} theme={theme} />
          )}
          </UserContext.Consumer>
          )}
          </ThemeContext.Consumer>
          );
          }


          To use functions in context in your component you'd typically wrap your component in a HOC so the context is passed in as props:



          export const withThemeContext = Component => (
          props => (
          <ThemeContext.Consumer>
          {context => <Component themeContext={context} {...props} />}
          </ThemeContext.Consumer>
          )
          )

          const YourComponent = ({ themeContext, ...props }) => {
          themeContext.someFunction()
          return (<div>Hi Mom!</div>)
          }

          export default withThemeContext(YourComponent)





          share|improve this answer























          • The context is giving me functions to be used in the class, not data to be rendered.
            – ATOzTOA
            Nov 16 at 23:29










          • @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
            – coreyward
            Nov 16 at 23:59










          • When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
            – ATOzTOA
            Nov 17 at 5:13











          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',
          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%2f53346462%2freact-multiple-contexts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can still use function-as-a-child consumer nodes with the 16.3 Context API, which is what the React documentation suggests doing:



          // Theme context, default to light theme
          const ThemeContext = React.createContext('light');

          // Signed-in user context
          const UserContext = React.createContext({
          name: 'Guest',
          });

          class App extends React.Component {
          render() {
          const {signedInUser, theme} = this.props;

          // App component that provides initial context values
          return (
          <ThemeContext.Provider value={theme}>
          <UserContext.Provider value={signedInUser}>
          <Layout />
          </UserContext.Provider>
          </ThemeContext.Provider>
          );
          }
          }

          function Layout() {
          return (
          <div>
          <Sidebar />
          <Content />
          </div>
          );
          }

          // A component may consume multiple contexts
          function Content() {
          return (
          <ThemeContext.Consumer>
          {theme => (
          <UserContext.Consumer>
          {user => (
          <ProfilePage user={user} theme={theme} />
          )}
          </UserContext.Consumer>
          )}
          </ThemeContext.Consumer>
          );
          }


          To use functions in context in your component you'd typically wrap your component in a HOC so the context is passed in as props:



          export const withThemeContext = Component => (
          props => (
          <ThemeContext.Consumer>
          {context => <Component themeContext={context} {...props} />}
          </ThemeContext.Consumer>
          )
          )

          const YourComponent = ({ themeContext, ...props }) => {
          themeContext.someFunction()
          return (<div>Hi Mom!</div>)
          }

          export default withThemeContext(YourComponent)





          share|improve this answer























          • The context is giving me functions to be used in the class, not data to be rendered.
            – ATOzTOA
            Nov 16 at 23:29










          • @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
            – coreyward
            Nov 16 at 23:59










          • When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
            – ATOzTOA
            Nov 17 at 5:13















          up vote
          2
          down vote



          accepted










          You can still use function-as-a-child consumer nodes with the 16.3 Context API, which is what the React documentation suggests doing:



          // Theme context, default to light theme
          const ThemeContext = React.createContext('light');

          // Signed-in user context
          const UserContext = React.createContext({
          name: 'Guest',
          });

          class App extends React.Component {
          render() {
          const {signedInUser, theme} = this.props;

          // App component that provides initial context values
          return (
          <ThemeContext.Provider value={theme}>
          <UserContext.Provider value={signedInUser}>
          <Layout />
          </UserContext.Provider>
          </ThemeContext.Provider>
          );
          }
          }

          function Layout() {
          return (
          <div>
          <Sidebar />
          <Content />
          </div>
          );
          }

          // A component may consume multiple contexts
          function Content() {
          return (
          <ThemeContext.Consumer>
          {theme => (
          <UserContext.Consumer>
          {user => (
          <ProfilePage user={user} theme={theme} />
          )}
          </UserContext.Consumer>
          )}
          </ThemeContext.Consumer>
          );
          }


          To use functions in context in your component you'd typically wrap your component in a HOC so the context is passed in as props:



          export const withThemeContext = Component => (
          props => (
          <ThemeContext.Consumer>
          {context => <Component themeContext={context} {...props} />}
          </ThemeContext.Consumer>
          )
          )

          const YourComponent = ({ themeContext, ...props }) => {
          themeContext.someFunction()
          return (<div>Hi Mom!</div>)
          }

          export default withThemeContext(YourComponent)





          share|improve this answer























          • The context is giving me functions to be used in the class, not data to be rendered.
            – ATOzTOA
            Nov 16 at 23:29










          • @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
            – coreyward
            Nov 16 at 23:59










          • When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
            – ATOzTOA
            Nov 17 at 5:13













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can still use function-as-a-child consumer nodes with the 16.3 Context API, which is what the React documentation suggests doing:



          // Theme context, default to light theme
          const ThemeContext = React.createContext('light');

          // Signed-in user context
          const UserContext = React.createContext({
          name: 'Guest',
          });

          class App extends React.Component {
          render() {
          const {signedInUser, theme} = this.props;

          // App component that provides initial context values
          return (
          <ThemeContext.Provider value={theme}>
          <UserContext.Provider value={signedInUser}>
          <Layout />
          </UserContext.Provider>
          </ThemeContext.Provider>
          );
          }
          }

          function Layout() {
          return (
          <div>
          <Sidebar />
          <Content />
          </div>
          );
          }

          // A component may consume multiple contexts
          function Content() {
          return (
          <ThemeContext.Consumer>
          {theme => (
          <UserContext.Consumer>
          {user => (
          <ProfilePage user={user} theme={theme} />
          )}
          </UserContext.Consumer>
          )}
          </ThemeContext.Consumer>
          );
          }


          To use functions in context in your component you'd typically wrap your component in a HOC so the context is passed in as props:



          export const withThemeContext = Component => (
          props => (
          <ThemeContext.Consumer>
          {context => <Component themeContext={context} {...props} />}
          </ThemeContext.Consumer>
          )
          )

          const YourComponent = ({ themeContext, ...props }) => {
          themeContext.someFunction()
          return (<div>Hi Mom!</div>)
          }

          export default withThemeContext(YourComponent)





          share|improve this answer














          You can still use function-as-a-child consumer nodes with the 16.3 Context API, which is what the React documentation suggests doing:



          // Theme context, default to light theme
          const ThemeContext = React.createContext('light');

          // Signed-in user context
          const UserContext = React.createContext({
          name: 'Guest',
          });

          class App extends React.Component {
          render() {
          const {signedInUser, theme} = this.props;

          // App component that provides initial context values
          return (
          <ThemeContext.Provider value={theme}>
          <UserContext.Provider value={signedInUser}>
          <Layout />
          </UserContext.Provider>
          </ThemeContext.Provider>
          );
          }
          }

          function Layout() {
          return (
          <div>
          <Sidebar />
          <Content />
          </div>
          );
          }

          // A component may consume multiple contexts
          function Content() {
          return (
          <ThemeContext.Consumer>
          {theme => (
          <UserContext.Consumer>
          {user => (
          <ProfilePage user={user} theme={theme} />
          )}
          </UserContext.Consumer>
          )}
          </ThemeContext.Consumer>
          );
          }


          To use functions in context in your component you'd typically wrap your component in a HOC so the context is passed in as props:



          export const withThemeContext = Component => (
          props => (
          <ThemeContext.Consumer>
          {context => <Component themeContext={context} {...props} />}
          </ThemeContext.Consumer>
          )
          )

          const YourComponent = ({ themeContext, ...props }) => {
          themeContext.someFunction()
          return (<div>Hi Mom!</div>)
          }

          export default withThemeContext(YourComponent)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 16:28

























          answered Nov 16 at 23:07









          coreyward

          49.2k1595124




          49.2k1595124












          • The context is giving me functions to be used in the class, not data to be rendered.
            – ATOzTOA
            Nov 16 at 23:29










          • @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
            – coreyward
            Nov 16 at 23:59










          • When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
            – ATOzTOA
            Nov 17 at 5:13


















          • The context is giving me functions to be used in the class, not data to be rendered.
            – ATOzTOA
            Nov 16 at 23:29










          • @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
            – coreyward
            Nov 16 at 23:59










          • When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
            – ATOzTOA
            Nov 17 at 5:13
















          The context is giving me functions to be used in the class, not data to be rendered.
          – ATOzTOA
          Nov 16 at 23:29




          The context is giving me functions to be used in the class, not data to be rendered.
          – ATOzTOA
          Nov 16 at 23:29












          @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
          – coreyward
          Nov 16 at 23:59




          @ATOzTOA This works the same with multiple context providers as it does like normal, but I updated my answer with a quick example.
          – coreyward
          Nov 16 at 23:59












          When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
          – ATOzTOA
          Nov 17 at 5:13




          When YourComponent is a class, then themeContext becomes this.props.themeContext, right?
          – ATOzTOA
          Nov 17 at 5:13


















          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%2f53346462%2freact-multiple-contexts%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin