How to get multiple static contexts in new CONTEXT API in React v16.6












2















Hi I'm trying to access multiple contexts in a component but I got success with only one context value from provider. there are two providers ListContext and `MappingContext. How can I access contexts like this:



class TableData extends React.Component {
static contextType = ListContext;
static contextType = MappingContext;

componentDidMount() {
const data = this.context // it will have only one context from ListContext
}


I know I can use multiple providers in render() but I want to access the contexts like above. Any help will be appreciated.



Thanks










share|improve this question



























    2















    Hi I'm trying to access multiple contexts in a component but I got success with only one context value from provider. there are two providers ListContext and `MappingContext. How can I access contexts like this:



    class TableData extends React.Component {
    static contextType = ListContext;
    static contextType = MappingContext;

    componentDidMount() {
    const data = this.context // it will have only one context from ListContext
    }


    I know I can use multiple providers in render() but I want to access the contexts like above. Any help will be appreciated.



    Thanks










    share|improve this question

























      2












      2








      2








      Hi I'm trying to access multiple contexts in a component but I got success with only one context value from provider. there are two providers ListContext and `MappingContext. How can I access contexts like this:



      class TableData extends React.Component {
      static contextType = ListContext;
      static contextType = MappingContext;

      componentDidMount() {
      const data = this.context // it will have only one context from ListContext
      }


      I know I can use multiple providers in render() but I want to access the contexts like above. Any help will be appreciated.



      Thanks










      share|improve this question














      Hi I'm trying to access multiple contexts in a component but I got success with only one context value from provider. there are two providers ListContext and `MappingContext. How can I access contexts like this:



      class TableData extends React.Component {
      static contextType = ListContext;
      static contextType = MappingContext;

      componentDidMount() {
      const data = this.context // it will have only one context from ListContext
      }


      I know I can use multiple providers in render() but I want to access the contexts like above. Any help will be appreciated.



      Thanks







      javascript reactjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 31 '18 at 13:46









      Sakhi MansoorSakhi Mansoor

      2,5682419




      2,5682419
























          2 Answers
          2






          active

          oldest

          votes


















          3














          One workaround is to use a wrapper that combines the two contexts into one and then export the wrapper. There are multiple ways to implement the wrapper, but here is one:



          Contexts.js



          import React from "react";

          export const Context1 = React.createContext("1");
          export const Context2 = React.createContext("2");
          export const ContextCombined1And2 = React.createContext("3");


          ProvideCombinedContext.js



          import React from "react";
          import { Context1, Context2, ContextCombined1And2 } from "./Contexts";

          // This is a reusable piece that could be used by any component that requires both contexts.
          const ProvideCombinedContext = props => {
          return (
          <Context1.Consumer>
          {context1 => (
          <Context2.Consumer>
          {context2 => (
          <ContextCombined1And2.Provider value={{ context1, context2 }}>
          {props.children}
          </ContextCombined1And2.Provider>
          )}
          </Context2.Consumer>
          )}
          </Context1.Consumer>
          );
          };
          export default ProvideCombinedContext;


          Need2Contexts.js



          import React from "react";
          import { ContextCombined1And2 } from "./Contexts";
          import ProvideCombinedContext from "./ProvideCombinedContext";

          class Need2Contexts extends React.Component {
          static contextType = ContextCombined1And2;
          componentDidMount() {
          console.log("Context=" + JSON.stringify(this.context));
          }
          render() {
          return "this.context=" + JSON.stringify(this.context);
          }
          }

          const WrappedNeed2Contexts = props => {
          return (
          <ProvideCombinedContext>
          <Need2Contexts {...props} />
          </ProvideCombinedContext>
          );
          };

          export default WrappedNeed2Contexts;


          index.js



          import React from "react";
          import ReactDOM from "react-dom";
          import { Context1, Context2 } from "./Contexts";
          import Need2Contexts from "./Need2Contexts";

          function App() {
          return (
          <div className="App">
          <Context1.Provider value="value1">
          <Context2.Provider value="value2">
          <Need2Contexts />
          </Context2.Provider>
          </Context1.Provider>
          </div>
          );
          }

          const rootElement = document.getElementById("root");
          ReactDOM.render(<App />, rootElement);


          You can see this in action and play with it here:



          Edit xv373l5ynz






          share|improve this answer

































            3














            This is explained in the React context documentation:




            You can only subscribe to a single context using this API. If you need to read more than one see Consuming Multiple Contexts.







            share|improve this answer
























            • Yeah. But what is the workaround if I need both contexts in componentDidMount?

              – Sakhi Mansoor
              Dec 31 '18 at 13:59











            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%2f53988193%2fhow-to-get-multiple-static-contexts-in-new-context-api-in-react-v16-6%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            One workaround is to use a wrapper that combines the two contexts into one and then export the wrapper. There are multiple ways to implement the wrapper, but here is one:



            Contexts.js



            import React from "react";

            export const Context1 = React.createContext("1");
            export const Context2 = React.createContext("2");
            export const ContextCombined1And2 = React.createContext("3");


            ProvideCombinedContext.js



            import React from "react";
            import { Context1, Context2, ContextCombined1And2 } from "./Contexts";

            // This is a reusable piece that could be used by any component that requires both contexts.
            const ProvideCombinedContext = props => {
            return (
            <Context1.Consumer>
            {context1 => (
            <Context2.Consumer>
            {context2 => (
            <ContextCombined1And2.Provider value={{ context1, context2 }}>
            {props.children}
            </ContextCombined1And2.Provider>
            )}
            </Context2.Consumer>
            )}
            </Context1.Consumer>
            );
            };
            export default ProvideCombinedContext;


            Need2Contexts.js



            import React from "react";
            import { ContextCombined1And2 } from "./Contexts";
            import ProvideCombinedContext from "./ProvideCombinedContext";

            class Need2Contexts extends React.Component {
            static contextType = ContextCombined1And2;
            componentDidMount() {
            console.log("Context=" + JSON.stringify(this.context));
            }
            render() {
            return "this.context=" + JSON.stringify(this.context);
            }
            }

            const WrappedNeed2Contexts = props => {
            return (
            <ProvideCombinedContext>
            <Need2Contexts {...props} />
            </ProvideCombinedContext>
            );
            };

            export default WrappedNeed2Contexts;


            index.js



            import React from "react";
            import ReactDOM from "react-dom";
            import { Context1, Context2 } from "./Contexts";
            import Need2Contexts from "./Need2Contexts";

            function App() {
            return (
            <div className="App">
            <Context1.Provider value="value1">
            <Context2.Provider value="value2">
            <Need2Contexts />
            </Context2.Provider>
            </Context1.Provider>
            </div>
            );
            }

            const rootElement = document.getElementById("root");
            ReactDOM.render(<App />, rootElement);


            You can see this in action and play with it here:



            Edit xv373l5ynz






            share|improve this answer






























              3














              One workaround is to use a wrapper that combines the two contexts into one and then export the wrapper. There are multiple ways to implement the wrapper, but here is one:



              Contexts.js



              import React from "react";

              export const Context1 = React.createContext("1");
              export const Context2 = React.createContext("2");
              export const ContextCombined1And2 = React.createContext("3");


              ProvideCombinedContext.js



              import React from "react";
              import { Context1, Context2, ContextCombined1And2 } from "./Contexts";

              // This is a reusable piece that could be used by any component that requires both contexts.
              const ProvideCombinedContext = props => {
              return (
              <Context1.Consumer>
              {context1 => (
              <Context2.Consumer>
              {context2 => (
              <ContextCombined1And2.Provider value={{ context1, context2 }}>
              {props.children}
              </ContextCombined1And2.Provider>
              )}
              </Context2.Consumer>
              )}
              </Context1.Consumer>
              );
              };
              export default ProvideCombinedContext;


              Need2Contexts.js



              import React from "react";
              import { ContextCombined1And2 } from "./Contexts";
              import ProvideCombinedContext from "./ProvideCombinedContext";

              class Need2Contexts extends React.Component {
              static contextType = ContextCombined1And2;
              componentDidMount() {
              console.log("Context=" + JSON.stringify(this.context));
              }
              render() {
              return "this.context=" + JSON.stringify(this.context);
              }
              }

              const WrappedNeed2Contexts = props => {
              return (
              <ProvideCombinedContext>
              <Need2Contexts {...props} />
              </ProvideCombinedContext>
              );
              };

              export default WrappedNeed2Contexts;


              index.js



              import React from "react";
              import ReactDOM from "react-dom";
              import { Context1, Context2 } from "./Contexts";
              import Need2Contexts from "./Need2Contexts";

              function App() {
              return (
              <div className="App">
              <Context1.Provider value="value1">
              <Context2.Provider value="value2">
              <Need2Contexts />
              </Context2.Provider>
              </Context1.Provider>
              </div>
              );
              }

              const rootElement = document.getElementById("root");
              ReactDOM.render(<App />, rootElement);


              You can see this in action and play with it here:



              Edit xv373l5ynz






              share|improve this answer




























                3












                3








                3







                One workaround is to use a wrapper that combines the two contexts into one and then export the wrapper. There are multiple ways to implement the wrapper, but here is one:



                Contexts.js



                import React from "react";

                export const Context1 = React.createContext("1");
                export const Context2 = React.createContext("2");
                export const ContextCombined1And2 = React.createContext("3");


                ProvideCombinedContext.js



                import React from "react";
                import { Context1, Context2, ContextCombined1And2 } from "./Contexts";

                // This is a reusable piece that could be used by any component that requires both contexts.
                const ProvideCombinedContext = props => {
                return (
                <Context1.Consumer>
                {context1 => (
                <Context2.Consumer>
                {context2 => (
                <ContextCombined1And2.Provider value={{ context1, context2 }}>
                {props.children}
                </ContextCombined1And2.Provider>
                )}
                </Context2.Consumer>
                )}
                </Context1.Consumer>
                );
                };
                export default ProvideCombinedContext;


                Need2Contexts.js



                import React from "react";
                import { ContextCombined1And2 } from "./Contexts";
                import ProvideCombinedContext from "./ProvideCombinedContext";

                class Need2Contexts extends React.Component {
                static contextType = ContextCombined1And2;
                componentDidMount() {
                console.log("Context=" + JSON.stringify(this.context));
                }
                render() {
                return "this.context=" + JSON.stringify(this.context);
                }
                }

                const WrappedNeed2Contexts = props => {
                return (
                <ProvideCombinedContext>
                <Need2Contexts {...props} />
                </ProvideCombinedContext>
                );
                };

                export default WrappedNeed2Contexts;


                index.js



                import React from "react";
                import ReactDOM from "react-dom";
                import { Context1, Context2 } from "./Contexts";
                import Need2Contexts from "./Need2Contexts";

                function App() {
                return (
                <div className="App">
                <Context1.Provider value="value1">
                <Context2.Provider value="value2">
                <Need2Contexts />
                </Context2.Provider>
                </Context1.Provider>
                </div>
                );
                }

                const rootElement = document.getElementById("root");
                ReactDOM.render(<App />, rootElement);


                You can see this in action and play with it here:



                Edit xv373l5ynz






                share|improve this answer















                One workaround is to use a wrapper that combines the two contexts into one and then export the wrapper. There are multiple ways to implement the wrapper, but here is one:



                Contexts.js



                import React from "react";

                export const Context1 = React.createContext("1");
                export const Context2 = React.createContext("2");
                export const ContextCombined1And2 = React.createContext("3");


                ProvideCombinedContext.js



                import React from "react";
                import { Context1, Context2, ContextCombined1And2 } from "./Contexts";

                // This is a reusable piece that could be used by any component that requires both contexts.
                const ProvideCombinedContext = props => {
                return (
                <Context1.Consumer>
                {context1 => (
                <Context2.Consumer>
                {context2 => (
                <ContextCombined1And2.Provider value={{ context1, context2 }}>
                {props.children}
                </ContextCombined1And2.Provider>
                )}
                </Context2.Consumer>
                )}
                </Context1.Consumer>
                );
                };
                export default ProvideCombinedContext;


                Need2Contexts.js



                import React from "react";
                import { ContextCombined1And2 } from "./Contexts";
                import ProvideCombinedContext from "./ProvideCombinedContext";

                class Need2Contexts extends React.Component {
                static contextType = ContextCombined1And2;
                componentDidMount() {
                console.log("Context=" + JSON.stringify(this.context));
                }
                render() {
                return "this.context=" + JSON.stringify(this.context);
                }
                }

                const WrappedNeed2Contexts = props => {
                return (
                <ProvideCombinedContext>
                <Need2Contexts {...props} />
                </ProvideCombinedContext>
                );
                };

                export default WrappedNeed2Contexts;


                index.js



                import React from "react";
                import ReactDOM from "react-dom";
                import { Context1, Context2 } from "./Contexts";
                import Need2Contexts from "./Need2Contexts";

                function App() {
                return (
                <div className="App">
                <Context1.Provider value="value1">
                <Context2.Provider value="value2">
                <Need2Contexts />
                </Context2.Provider>
                </Context1.Provider>
                </div>
                );
                }

                const rootElement = document.getElementById("root");
                ReactDOM.render(<App />, rootElement);


                You can see this in action and play with it here:



                Edit xv373l5ynz







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 1 at 12:34

























                answered Dec 31 '18 at 14:57









                Ryan CogswellRyan Cogswell

                3,509419




                3,509419

























                    3














                    This is explained in the React context documentation:




                    You can only subscribe to a single context using this API. If you need to read more than one see Consuming Multiple Contexts.







                    share|improve this answer
























                    • Yeah. But what is the workaround if I need both contexts in componentDidMount?

                      – Sakhi Mansoor
                      Dec 31 '18 at 13:59
















                    3














                    This is explained in the React context documentation:




                    You can only subscribe to a single context using this API. If you need to read more than one see Consuming Multiple Contexts.







                    share|improve this answer
























                    • Yeah. But what is the workaround if I need both contexts in componentDidMount?

                      – Sakhi Mansoor
                      Dec 31 '18 at 13:59














                    3












                    3








                    3







                    This is explained in the React context documentation:




                    You can only subscribe to a single context using this API. If you need to read more than one see Consuming Multiple Contexts.







                    share|improve this answer













                    This is explained in the React context documentation:




                    You can only subscribe to a single context using this API. If you need to read more than one see Consuming Multiple Contexts.








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 31 '18 at 13:57









                    keulkeul

                    5,6451030




                    5,6451030













                    • Yeah. But what is the workaround if I need both contexts in componentDidMount?

                      – Sakhi Mansoor
                      Dec 31 '18 at 13:59



















                    • Yeah. But what is the workaround if I need both contexts in componentDidMount?

                      – Sakhi Mansoor
                      Dec 31 '18 at 13:59

















                    Yeah. But what is the workaround if I need both contexts in componentDidMount?

                    – Sakhi Mansoor
                    Dec 31 '18 at 13:59





                    Yeah. But what is the workaround if I need both contexts in componentDidMount?

                    – Sakhi Mansoor
                    Dec 31 '18 at 13:59


















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988193%2fhow-to-get-multiple-static-contexts-in-new-context-api-in-react-v16-6%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

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS