<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rajesh Jain</title>
    <description>The latest articles on Forem by Rajesh Jain (@rjain15).</description>
    <link>https://forem.com/rjain15</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F198093%2Fcad73973-0992-4993-8f69-c847534bdf2c.jpeg</url>
      <title>Forem: Rajesh Jain</title>
      <link>https://forem.com/rjain15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rjain15"/>
    <language>en</language>
    <item>
      <title>React Hooks - useContext and State</title>
      <dc:creator>Rajesh Jain</dc:creator>
      <pubDate>Mon, 22 Jul 2019 23:48:35 +0000</pubDate>
      <link>https://forem.com/rjain15/react-hooks-usecontext-and-state-7in</link>
      <guid>https://forem.com/rjain15/react-hooks-usecontext-and-state-7in</guid>
      <description>&lt;p&gt;I am using useContext react hook, and when I get the context from the useContext, is there a way to access the state?&lt;/p&gt;

&lt;p&gt;I might be mixing up things, &lt;/p&gt;

&lt;p&gt;here is my global state component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;GlobalContext.Provider
      value={{
        loadingProducts: state.loadingProducts,
        products: state.products,
        cart: state.cart,
        searchTerm: state.searchTerm,
        addProductToCart: addProductToCart,
        removeProductFromCart: removeProductFromCart,
        showAllProducts: showAllProducts,
        showFilteredProducts: showFilteredProducts,
        initCatalog: initCatalog,
        catalogLoadSuccess: catalogLoadSuccess,
        catalogLoadFailure: catalogLoadFailure

      }}
    &amp;gt;
      {props.children}
    &amp;lt;/GlobalContext.Provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and here is my initial state object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialState = {
  initialProducts: [],
  products: [],
  cart: [],
  searchTerm: '',
  loadingProducts: true
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have initialized the Global State&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const GlobalState = props =&amp;gt; {
  const [state, dispatch] = useReducer(reducer, initialState);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I want to get to the state object (loadingProducts) from my component and this is how I am getting it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const context = useContext(GlobalContext);
  const {loadingProducts} = context.loadingProducts

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Question: Can I access the state directly from context, in my component&lt;br&gt;
ie. &lt;/p&gt;

&lt;p&gt;const context = useContext(GlobalContext);&lt;/p&gt;

&lt;p&gt;// Or is there an alternative?&lt;br&gt;
  const state = context.state&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>React Hooks - useContext with multiple context</title>
      <dc:creator>Rajesh Jain</dc:creator>
      <pubDate>Fri, 19 Jul 2019 20:59:40 +0000</pubDate>
      <link>https://forem.com/rjain15/react-hooks-usecontext-with-multiple-context-32n3</link>
      <guid>https://forem.com/rjain15/react-hooks-usecontext-with-multiple-context-32n3</guid>
      <description>&lt;p&gt;Here is my question/confusion. &lt;/p&gt;

&lt;p&gt;I am following some code examples to create GlobalState and then use the react Hooks useContext to access the state. &lt;/p&gt;

&lt;p&gt;here is the example code&lt;br&gt;
&lt;a href="https://github.com/academind/react-redux-vs-context/blob/context-hooks/src/context/GlobalState.js"&gt;https://github.com/academind/react-redux-vs-context/blob/context-hooks/src/context/GlobalState.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and Example video&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=R_7XRX7nLsw"&gt;https://www.youtube.com/watch?v=R_7XRX7nLsw&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My question is, if I have multiple components, each trying to access the global state, do I create a global context or do I create multiple context. And then how do I use the provider and consumer if I have multiple context. &lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
