<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayush Jain on Medium]]></title>
        <description><![CDATA[Stories by Ayush Jain on Medium]]></description>
        <link>https://medium.com/@ayush.1996.jain?source=rss-9f89321e5474------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A2dji-OQEnBcxme04f2Qjg.jpeg</url>
            <title>Stories by Ayush Jain on Medium</title>
            <link>https://medium.com/@ayush.1996.jain?source=rss-9f89321e5474------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:46:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ayush.1996.jain/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Context API for Theme Switch]]></title>
            <link>https://medium.com/@ayush.1996.jain/context-api-for-theme-switch-c96c8bb0ba89?source=rss-9f89321e5474------2</link>
            <guid isPermaLink="false">https://medium.com/p/c96c8bb0ba89</guid>
            <dc:creator><![CDATA[Ayush Jain]]></dc:creator>
            <pubDate>Wed, 08 Apr 2020 14:17:04 GMT</pubDate>
            <atom:updated>2020-04-08T14:17:04.053Z</atom:updated>
            <content:encoded><![CDATA[<p>In a typical React application, data is passed top-down (parent to child) via props, but this can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree.</p><p>Context is designed to share data that can be considered “global” for a tree of React components,</p><p>Now let’s take an example you have to pass some value from one component to another you simply pass value using props. But now you have to pass value from component A to component Z and there is Z is the last child and A is the parent. So you have to pass value from A to B then B to C respectively, this will increase code complexity because let suppose we don’t want the value at C but C is the parent of Z so we have to pass value from C also this will increase the line of code. It’s also annoying that whenever the Value needs more props from the top, you have to add them at all the intermediate levels too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*yTxDl-NAWXbjeFyIRccRag.png" /><figcaption>Difference between Props Data passing and Context API data passing approach</figcaption></figure><p>In this example, we can observe that what is the difference between the, when we pass data using props and when we pass data from Context API</p><p>In Prop dealing, we have to pass data to all levels from top to down rather than in context API we make the data accessible to container component than we can access the data in any component in the project.</p><h3><strong>When to Use Context</strong></h3><p>Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it makes component reuse more difficult.</p><p>If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context.</p><h3><strong>How to use Context API</strong></h3><blockquote><strong>1) Create Context API</strong></blockquote><p>Creates a Context object. When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the tree.</p><p>The defaultValue argument is only used when a component does not have a matching Provider above it in the tree</p><blockquote><strong>2) Context Provider</strong></blockquote><p>a) Here Value may be anything, It may be a variable a function whatever the information you have to pass</p><p>b) One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree.</p><p>c) All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes</p><blockquote><strong>3) Context Consumer</strong></blockquote><p>Requires a function as a child. The function receives the current context value and returns a React node. The value argument passed to the function will be equal to the value prop of the closest Provider for this context above in the tree. If there is no Provider for this context above, the value argument will be equal to the defaultValue that was passed to createContext().</p><h3>Use Of Context API in theme change</h3><p>We want to build a “theme toggler” component which toggles between light mode and dark mode for our React app. Every component has to have access to the current theme mode so they can be styled accordingly.</p><p>Normally, we would provide the current theme mode to all the components through props and update the current theme using state:</p><blockquote><strong>1) ThemeContext</strong></blockquote><p>First, we make context provider for theme change that allows consuming components to subscribe to context changes. We define the default value in the provider that is the value for the first time load or until there is no change in context value</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*SRw14O50gJ3j1D6r-VDtFg.png" /><figcaption><strong>ThemeContext</strong></figcaption></figure><blockquote><strong>2) AuthContext</strong></blockquote><p>Similarly like theme context, we create an Auth Context which shows about the login and logout and we set the default value for them</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*i_fVnNUgwny2A-dX_3AJ1w.png" /><figcaption><strong>AuthContext</strong></figcaption></figure><blockquote><strong>3) APP</strong></blockquote><p>Go to your App.js file. To create our provider, we have to import our ThemeContext and AuthContext.</p><p>Once the ThemeContext and AuthContext have been imported, we have to enclose the context of our App component in ThemeContext. We enclose all components to ThemeContext and AuthContext for the component we want to make available data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*m0xaa-9RFVumLzfKyFTq9Q.png" /><figcaption><strong>APP</strong></figcaption></figure><blockquote><strong>4) NavBar</strong></blockquote><p>Now we can consume provider values in two ways using class component and using functional component In this component Nabar we using the class component. First, we have to import the context after that we assign the theme light or dark based on the isLightTheme value and isLightTheme value is provided using the provider and we will consume it</p><p>const {isLightTheme, light, dark} = themeContext (Using this we are consume the value)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*q9TNvckW5t46KvNPl0GyWg.png" /><figcaption><strong>NavBar</strong></figcaption></figure><blockquote><strong>5) BookList</strong></blockquote><p>Same for the BookList we assign the theme dark or light based on isLightTheme value so if isLightTheme value is true then we assign the theme light otherwise dark</p><p>In this component we are using functional component so, here we will use the hooks called useContext</p><p>const {isLightTheme,light,dark} = useContext(ThemeContext) (By these we are consuming value)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*C1xOOun9g3BS1rYzsf5RwQ.png" /><figcaption><strong>BookList</strong></figcaption></figure><blockquote><strong>6)ThemeToggle</strong></blockquote><p>ThemeToggle is used to change the isLightTheme value using the function toggleTheme the function which is provided using the Context Provider so when we call toggleTheme function is directly call the ThemeContext Provider function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*p_-n6PQY9rtCPcn8oyad2Q.png" /><figcaption><strong>ThemeToggle</strong></figcaption></figure><p>toggleTheme= () =&gt; {this.setState({ isLightTheme: !this.state.isLightTheme }); }</p><p>this function will set opposite to value of isLightTheme</p><p>1) By default it’s in light Theme</p><p>2) After Toggle the theme it’s theme change into Dark</p><h3><strong>Wrap Up</strong></h3><p>In this way, we can switch the theme using Context API. We can use Context for Langauge Translation, Route Change. Context is only used when some data needs to be accessible by many components at different nesting levels. There is no need to use context for passing data to one or two levels in this case you can use props.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c96c8bb0ba89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 Important Lessons I’ve Learned While Working With React.js]]></title>
            <link>https://medium.com/@ayush.1996.jain/5-important-lessons-ive-learned-while-working-with-react-js-aaf197da7e22?source=rss-9f89321e5474------2</link>
            <guid isPermaLink="false">https://medium.com/p/aaf197da7e22</guid>
            <category><![CDATA[js]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Ayush Jain]]></dc:creator>
            <pubDate>Tue, 18 Feb 2020 18:00:00 GMT</pubDate>
            <atom:updated>2020-02-18T18:01:46.052Z</atom:updated>
            <content:encoded><![CDATA[<p>Working with new technology can sometimes be too difficult and sometimes too easy. You watch too many tutorials to take opinions from other people. However, it’s up to you how you adapt to new technology.</p><p>I remember the first time, when I started learning React.JS, it was hard and a bit complex too. As I was new to JavaScript, I was limited to its basics only. If you are starting to learn React, first of all, you should learn to think in React then only your mindset will adapt to it.</p><p>In this blog, I’m sharing my personal experience with React in this article-</p><p>Let’s go!!</p><h3>#1. Stay Up to Date</h3><p>The latest version of React has brought several new features, bug fixes, and new deprecation warnings to help prepare for future major releases. So, you need to keep aware of it and stay up to date. Some of the examples are-</p><p><strong>Lifecycle Methods have changed-</strong></p><ul><li>componentWillMount → UNSAFE_componentWillMount</li><li>componentWillReceiveProps → UNSAFE_componentWillReceiveProps</li><li>componentWillUpdate → UNSAFE_componentWillUpdate</li></ul><p>You should adopt these changes because if you don’t, it will throw some warning which is not good for the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*hZnrHwretdXABUVwv2xjoQ.png" /><figcaption>Lifecycle Warning</figcaption></figure><p><strong>Deprecating “Factory” Components</strong></p><p>React had support for a “factory” component that returns an object with a render method</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*_xcD8WwgFxPD7j5FoqZ4yw.png" /><figcaption>Factory Components</figcaption></figure><p>This pattern was rarely used in the wild, thus, supporting it would cause React to be slightly larger and slower than necessary. If you rely on it, adding <strong><em>FactoryComponent.prototype = React.Component.prototype</em></strong> can serve as a workaround. Alternatively, you can convert it either to a class or to a function component.</p><p>Lately, so many new features have been introduced to React which we must have to be aware of and implement them in the projects.</p><h3>#2. Split Your Code into Smaller Chunks</h3><p>React is all about the components. So, you have to split bigger pieces into smaller ones. Make it so that if a new person comes into your project, they won’t need days to learn how everything runs.</p><p>It’s all about the readability and scalability of your code. If the code has fewer code smells and duplicity, it’s a win-win situation for everyone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*Fi9mxR1sd6PWB4zT5YG-Zg.png" /><figcaption>Code into smaller chunks</figcaption></figure><p>In the above example, the properties are static. So, we can have a pure component that is responsible for the website’s toaster message.</p><p>We can use it anywhere in code, using-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*YOcuthLlMnKbB5CM8fDDzw.png" /></figure><p>Similarly, we can use it for others as well, like-</p><ul><li>Error Message</li><li>Page Loader</li><li>Input</li><li>Button</li></ul><h3>#3. Always Be Advanced</h3><p>React is a very developing language, every day some new features and concepts are introduced in it. You must introduce these advanced features to your project.</p><p>Most of the people either don’t want to take the challenge or are not eager to put some extra effort to add these new concepts in their projects. But, you should not worry about it and always be ready to take the challenge.</p><p><strong>There are many terms you can explore like-</strong></p><ul><li>High Order component</li><li>Error Boundaries</li><li>Lazy Loading</li></ul><p>Also, don’t be afraid to try something new at your work (within certain boundaries, of course!). Don’t just experiment on personal projects. People will argue, you just need to have a strong point to prove yourself.</p><p><strong>Use lazy loading like this-</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/0*nGLMG59y0H-31lpk.png" /><figcaption>Lazy Loading</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/0*3hsrGthjkXlUBSS0.png" /></figure><h3>#4. Refactor Again and Again</h3><p>Our mind is changed many times, sometimes client also changes their Requirement, project Manager too even the tester can suggest some improvement. People will criticize your work, as a result, you will have to change your code, many times. But it’s a natural process, code is never free of error. For this, we have to refactor again and again the same code.</p><p>The very important thing is never to forget to test your software. Do testing every time you make even a small change — don’t be shy of them.</p><h3>#5. Don’t Over-Complicate Things</h3><p>Write code that is easy to understand and easy to use. If you don’t need Redux or something, and still using it because everyone uses it without understanding it’s true purpose, please don’t.</p><p>Sometimes you are using the latest technologies and writing complex code to show off, it’s really not needed, write code which is easy to read. Because in the end, you will understand that easier code which was written without showing off. Also, it’s easily understandable for others too.</p><p>When some other team members working on your code than for them it’s easy to complete that task without wasting too much time because code is reliable and not too complex. According to me, if your goal is to earn respect than improve your code for the team.</p><h3>Wrap Up:</h3><p>React is a very developing language and if you enjoy working in React.js you can learn many new topics, concepts and implement them in your projects.</p><p>Before JS, I was working in JAVA and it was difficult to adapt JS. But I was all full of excitement and after working with React.js, the excitement is same. React is like every day you learn something new.</p><p>Therefore, I would recommend everyone to ask themselves: “Do you enjoy what you do?”</p><p>If not, then search for that special piece. Because we have to find that special piece. Success cannot be forced, it must be achieved.</p><p><strong>Thanks for Reading!!</strong></p><p>By <a href="https://www.linkedin.com/in/ayush-jain-187009120/"><strong><em>Ayush Jain</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aaf197da7e22" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>