<?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 Furkan Bağırgan on Medium]]></title>
        <description><![CDATA[Stories by Furkan Bağırgan on Medium]]></description>
        <link>https://medium.com/@furkanbagirgan?source=rss-46f27cdeaf1f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Furkan Bağırgan on Medium</title>
            <link>https://medium.com/@furkanbagirgan?source=rss-46f27cdeaf1f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:52:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@furkanbagirgan/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[Why Props in components and not Context API?]]></title>
            <link>https://blog.devgenius.io/why-props-in-components-and-not-context-api-7749b6a9450a?source=rss-46f27cdeaf1f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7749b6a9450a</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[props-in-react]]></category>
            <category><![CDATA[context-api]]></category>
            <category><![CDATA[props]]></category>
            <dc:creator><![CDATA[Furkan Bağırgan]]></dc:creator>
            <pubDate>Mon, 18 Apr 2022 13:58:19 GMT</pubDate>
            <atom:updated>2022-04-19T16:20:55.975Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rxJCAHFwIwHUx6OuXNxsgg.png" /><figcaption>Props vs Context API</figcaption></figure><p>First of all, I would like to give information about the content of this article. The purpose of this article will not be to explain at length what context api or props are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*LbtOWH46WJ6xZfkjT_YSqw.gif" /></figure><p>I will only look for the answer to the question of where and when we should prefer them over each other. But I will focus more on why props are used in components, not context api.</p><p>The question of why we should prefer props or context api in components leaves us alone with the questions of whether it is good or not, as in everything else.</p><p>In such a situation, the decisions we make are sometimes very important for the work we do. As always, I would like to point out that many factors are important before answering the questions “Is it good or the other is good?” For example, when or where questions are important factors for us to choose which ones.</p><p>If you want, first of all, after briefly explaining what context api and props are from React’s own documentation, let’s look for answers to these questions and answer why props are preferred to context api.</p><p><strong>What is Context API?</strong></p><blockquote>Context provides a way to pass data through the component tree without having to manually traverse the props at each level.¹</blockquote><p><strong>What is Props?</strong></p><blockquote>Conceptually, components are like JavaScript functions. Components optionally take inputs called “props” and return React elements to be displayed on the screen using these props.²</blockquote><p>Now, if we have briefly remembered what these are, we can move on to the questions of which one to use where and when.</p><p><strong>Where and when are Props used?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*JBsdL5yXFUvg4_hfJ8DjkA.jpeg" /><figcaption>Props-Parent to Child</figcaption></figure><p>Let’s start right away by explaining where and when props are used. As stated in the definition, props are a structure that covers the data transferred from the parent to the child in the component architecture in react. With this structure, we can transfer data from a parent to a child of it. But mostly our react applications consist of communication of more than one component with each other.</p><pre>function Welcome(props) {  <br>  return &lt;h1&gt;Hello, {props.name}&lt;/h1&gt;;<br>}<br><br>const element = &lt;Welcome name=&quot;Sara&quot; /&gt;;<br>ReactDOM.render(<br>  element,<br>  document.getElementById(&#39;root&#39;)<br>);</pre><p>Therefore, we can pass this data from one component to another only through its children. We cannot transfer from one parent to another. So we use props only when there is data flow between parent and child. This situation is also called props drilling. Thus, we learned where and when to use props.</p><p>Now let’s see why props should be used instead of context api.</p><ul><li>If our react application consists of a small number of components and these components do not contain many children.</li><li>We have many components, but these components are independent from each other, the props structure should be preferred. Because components that are independent of each other, rarely have a structure that covers each other.</li></ul><p>Thus, when we want to transfer data to only one component, we can easily transfer it with the help of props.</p><p>Well, as for where and when we should not use it. When the situation is the opposite of the situations I mentioned above, that is;</p><ul><li>If our application has many child components.</li><li>These components are dependent on each other.</li></ul><p>In such cases, if we prefer the props structure, both the time cost and workload of our React application will increase.</p><p><strong>Where and when are Context API used ?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*GRyG71lMnIeDtXL3SbOREQ.jpeg" /><figcaption>ContextAPI-wraps all components</figcaption></figure><p>In the cases I mentioned above, in cases where props should not be preferred, different ways are used depending on the scale of the application. Examples of these ways are Redux or Context API. I will explain the Context API way here.</p><p>First of all, as mentioned in the definition of the context api, by using this structure, we can access the data we want from anywhere in our application. In other words, it has a usage structure that can be accessed from anywhere. Before this, a context is created and the data and functions that we want the components to reach are kept there. Then, by wrapping our application with this context, we open our data to access from anywhere.</p><pre>const ThemeContext = React.createContext(&#39;light&#39;);</pre><pre>class App extends React.Component {<br>  render() {<br>    return (<br>      &lt;ThemeContext.Provider value=&quot;dark&quot;&gt;<br>        &lt;ThemedButton/&gt;<br>      &lt;/ThemeContext.Provider&gt;<br>    );<br>  }<br>}<br><br>class ThemedButton extends React.Component {<br>  static contextType = ThemeContext;<br>  render() {<br>    return &lt;Button theme={this.context} /&gt;;<br>  }<br>}</pre><p>This structure is generally used where;</p><ul><li>There are many components.</li><li>There are many parent and child components that are connected to each other.</li></ul><p>But there are some disadvantages in this structure. As I said at the beginning, something cannot be the best everywhere and always. One of its shortcomings;</p><ul><li>Context structure becomes very complex when the number of components increase.</li><li>The number of data we want to transfer to components increase.</li></ul><p>This is contrary to the difficulty of writing code for us and the structure we call clean code, but it also causes time and performance losses in our application. Therefore, in cases where the context api is not sufficient, we need to resort to more advanced structures. Like Redux and so on…</p><p><strong>As a result,</strong><br>Yes, now I think we have a little bit of information about where and when context api and props should be preferred. Thus, we understood why props are preferred over context api in components. Apart from some of the information I have mentioned here, there are many finer details. Of course, none of us can know all the points,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*8sM7ANyaBQK4jBd18M1A7Q.gif" /></figure><p>but we can decide for ourselves what works best.😉</p><p>I hope this article has been useful for where and when props and context api should be preferred for you. Or at least it has providing a basis for you. Also, if you want to get more technical information about context api and props, you can find React’s own documentation <a href="https://reactjs.org/">here</a>.</p><p>See you in my next post 👋</p><p><strong>References</strong></p><ol><li>React Docs| <a href="https://reactjs.org/docs/context.html">Context</a></li><li>React Docs| <a href="https://reactjs.org/docs/components-and-props.html">Components and Props</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7749b6a9450a" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/why-props-in-components-and-not-context-api-7749b6a9450a">Why Props in components and not Context API?</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>