<?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 Katya Weicht on Medium]]></title>
        <description><![CDATA[Stories by Katya Weicht on Medium]]></description>
        <link>https://medium.com/@katya.weicht?source=rss-6290bbe57960------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*YWByQVhIm-jIUq4KFq426Q.png</url>
            <title>Stories by Katya Weicht on Medium</title>
            <link>https://medium.com/@katya.weicht?source=rss-6290bbe57960------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:50:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@katya.weicht/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[React Component Types: Functional vs Class Components]]></title>
            <link>https://medium.com/@katya.weicht/react-component-types-functional-vs-class-components-31243661b3f7?source=rss-6290bbe57960------2</link>
            <guid isPermaLink="false">https://medium.com/p/31243661b3f7</guid>
            <category><![CDATA[react-state]]></category>
            <category><![CDATA[class-component]]></category>
            <category><![CDATA[react-event-handling]]></category>
            <category><![CDATA[functional-component]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Katya Weicht]]></dc:creator>
            <pubDate>Fri, 09 Jan 2026 18:28:56 GMT</pubDate>
            <atom:updated>2026-01-09T18:32:47.287Z</atom:updated>
            <content:encoded><![CDATA[<h4>Key differences every React learner should know</h4><h4><strong>Introduction to Components</strong></h4><p>Components are independent, reusable pieces of code that return JSX — a syntax extension to JavaScript that lets you write HTML-like code to render parts of the user interface. In React, there are two different types of components — functional components and class components. Both serve the same purpose but differ in syntax, state management, lifecycle methods, rendering, and event handling.</p><h4><strong>Basic Structure and Syntax</strong></h4><p>Functional components can be defined using two different JavaScript function syntaxes. This is purely a syntax difference and does not affect how the component behaves.</p><pre>const FunctionalComponent = () =&gt; {<br>  return (<br>    &lt;div&gt;<br>      &lt;p&gt;This is a functional component&lt;/p&gt;<br>    &lt;/div&gt;<br>  )<br>}<br>export default FunctionalComponent</pre><pre>function FunctionalComponent(){<br>  return (<br>    &lt;div&gt;<br>      &lt;p&gt;This is a functional component&lt;/p&gt;<br>    &lt;/div&gt;<br>  )<br>}<br>export default FunctionalComponent</pre><p>Unlike functional components, to use a class component you must first import the component class from the React library. While functional components return the JSX directly inside the function, class components use the render() method to return JSX.</p><pre>import {Component} from &quot;react&quot;<br><br>class ClassComponent extends Component{<br>  render(){<br>    return(<br>      &lt;div&gt;<br>        &lt;p&gt;This is a class component&lt;/p&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br>export default ClassComponent</pre><h4><strong>State Management</strong></h4><p>State is an object where values belonging to a component are stored. When state changes, the component re-renders.</p><p>In functional components, state is managed through hooks, which are not available in class components. To demonstrate how state is used I will create a functional component that displays the name of a color with a button to change the color.</p><pre>import {useState} from &quot;react&quot;<br><br>function StateManagement() {<br>  const [color,setColor] = useState(&quot;green&quot;)<br><br>  function handleClick() {<br>    setColor(&quot;blue&quot;)<br>  }<br><br>  return(<br>    &lt;div&gt;<br>      &lt;p&gt;{color}&lt;/p&gt;<br>      &lt;button onClick={handleClick}&gt;Change the color to blue&lt;/button&gt;<br>    &lt;/div&gt;<br>  )<br>}<br><br>export default StateManagement</pre><figure><img alt="A view of a webpage with the text “green” and a button below that labeled with the words “Change the color to blue”" src="https://cdn-images-1.medium.com/max/986/1*3FdanXHehwEWP6YJRvnENg.png" /><figcaption>View of what is rendered by the functional component above</figcaption></figure><p>The useState hook must first be imported from the React library in order to begin using it. The format is as follows:</p><p>const [state, setState] = useState(initialState)</p><p>The initial state is what you want the value of your state to be when the component first renders. This could be an object — useState({}), an array — useState([]), a number — useState(1), a string — useState(“pineapple”), or even null — useState(null). State is accessed through the variable state<em>, </em>which holds the current value of state. State is changed or updated with the setState function.</p><p>In the above example, the initial state is set as “green”, the current state is accessed through the state variable color<em> </em>and state is updated with the setColor function once the button is clicked. Because changes in state cause the component to re-render, the user will immediately see the color “blue” displayed on their screen in place of “green”.</p><p>It is important to be specific when naming your state variable and setState function as you may need to use the useState hook multiple times in a component.</p><pre>const [color, setColor] = useState(&quot;green&quot;)<br>const [name, setName] = useState(&quot;Alice&quot;)</pre><p>As a class component does not have access to hooks, state will be accessed through this.state and updated with<em> </em>the this.setState()<em> </em>method<em>. </em>Unlike a functional component, nothing needs to be imported in order to use and access state.</p><pre>import {Component} from &quot;react&quot;<br><br>class StateManagement extends Component{<br>  state = {<br>    color: &quot;green&quot;,<br>  }<br><br>  handleClick = () =&gt; {<br>   this.setState({color: &quot;blue&quot;})<br>  }<br><br>  render(){<br>    return(<br>      &lt;div&gt;<br>        &lt;p&gt;{this.state.color}&lt;/p&gt;<br>        &lt;button onClick={this.handleClick}&gt;Change the color to blue&lt;/button&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br>export default StateManagement</pre><p>In a functional component you can set the initial value of state to be a string such as “green”; however, in a class component, state must always be an object or the <em>setState</em> method will not work. As can be seen in the above example, the class component’s state is initially set as the object {color: “green”}. The color is then accessed using this.state.color.<em> </em>When the user clicks the button, the color stored in state is updated with the this.setState() method. The setState method also requires an object — this.setState({color: “blue}).</p><p>If your component needs to hold more than one value in state, you would simply add another key and value to your state object.</p><pre>state = {<br>  color: &quot;green&quot;,<br>  number: 1,<br>}</pre><p>The color would be accessed through this.state.color and the number would be accessed through this.state.number.</p><h4><strong>Lifecycle Methods</strong></h4><p>The three phases of a react lifecycle are mounting, updating, and unmounting. Mounting is when a component is created and rendered for the first time. During this phase, React renders the JSX returned by the component and updates the DOM(document object model). Updating occurs when a component re-renders due to changes in state or props(an object that holds data passed from a parent component to a child component). Unmounting is when a component is removed from the DOM.</p><p>In functional components, lifecycle behavior is handled using hooks like useEffect.</p><p>useEffect( ( ) =&gt; {…}, [ ])</p><p>In a useEffect hook, the code inside the hook will run either only once - when the component is mounted (if the dependency array is left empty) or the component will also re-render whenever a variable in the dependency array changes. The user may optionally return a cleanup function inside the hook that will run when the component unmounts. This single hook encompasses all three phases of the React lifecycle.</p><p>In this first example, the code will run only once when the component mounts because the dependency array is empty. The code in the return of the useEffect runs during the unmounting phase of the lifecycle.</p><pre>import {useEffect} from &quot;react&quot;<br><br>useEffect( () =&gt; {<br>  console.log(&quot;The component has mounted&quot;)<br>  return() =&gt; {<br>    console.log(&quot;Cleanup complete!&quot;) <br>  }<br>},[])</pre><p>In this second example, the code inside of the useEffect hook will run once when the component is first mounted. This is the mounting phase of the lifecycle. The code then runs again every time the state variable of color changes. When the user clicks a button to change the color, the state variable color<em> </em>changes, causing the component to re-render. This is the updating phase of the react lifecycle. In this code, there is nothing that requires a cleanup so the useEffect hook does not contain a cleanup function. The component unmounts and the React lifecycle is complete.</p><pre>import {useEffect, useState} from &quot;react&quot;<br><br>function LifecycleMethods(){<br>  const [color,setColor] = useState(&quot;green&quot;)<br><br>  useEffect( () =&gt; {<br>    console.log(`the color is ${color}`)<br>  },[color])<br><br>  function turnBlue() {<br>    setColor(&quot;blue&quot;)<br>  }<br><br>  function turnGreen() {<br>    setColor(&quot;green&quot;)<br>  }<br><br>  return(<br>    &lt;div&gt;<br>      &lt;p&gt;{color}&lt;/p&gt;<br>      &lt;button onClick={turnBlue}&gt;Change to Blue&lt;/button&gt;<br>      &lt;button onClick={turnGreen}&gt;Change to Green&lt;/button&gt;<br>    &lt;/div&gt;<br>  )<br>}<br>export default LifecycleMethods</pre><figure><img alt="A view of a webpage with the text “green” centered at the top and two buttons beneath it. The first button is blue and contains the text “Change to Blue”. The second button is green and contains the text “Change to Green”" src="https://cdn-images-1.medium.com/max/990/1*XjIH0ETE-tMZJEclddKXwg.png" /><figcaption>View of what is rendered by the functional component above</figcaption></figure><p>The useEffect hook in a functional component is unavailable to class components. Instead, class components have three lifecycle methods available to them: componentDidMount, componentDidUpdate, and componentWillUnmount</p><pre>import {Component} from &#39;react&#39;<br><br>class LifecycleMethods extends Component{<br>  state = {<br>    color: &quot;green&quot;,<br>    message: &quot;&quot;,<br>  }<br><br>  componentDidMount() {<br>    this.updateMessage()<br>  }<br><br>  componentDidUpdate(prevProps,prevState) {<br>    if(prevState.color !== this.state.color) {<br>      this.updateMessage()<br>    }<br>  }<br><br>  componentWillUnmount() {<br>    console.log(&quot;Nothing to clean up here&quot;)<br>  }<br><br>  updateMessage() {<br>    this.setState({<br>      message: `The color is currently ${this.state.color}!`<br>    })<br>  }<br><br>  turnBlue = () =&gt; {<br>    this.setState({color: &quot;blue&quot;})<br>  }<br><br>  turnGreen = () =&gt; {<br>    this.setState({color: &quot;green&quot;})<br>  }<br><br>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;p&gt;{this.state.color}&lt;/p&gt;<br>        &lt;button onClick={this.turnBlue}&gt;Change to Blue&lt;/button&gt;<br>        &lt;button onClick={this.turnGreen}&gt;Change to Green&lt;/button&gt;<br>        &lt;p&gt;{this.state.message}&lt;/p&gt;<br>      &lt;/div&gt;<br>    )<br>  }<br>}<br><br>export default LifecycleMethods</pre><p>In this class Component, state is initialized with color as “green” and message as an empty string. The componentDidMount method runs during the mounting phase of the React lifecycle and calls on the updateMessage method. This method sets the state variable message as “The color is currently (the current color)!”. The componentDidUpdate method runs during the updating phase of the lifecycle and updates the message with the new color if the color has been changed. The componentWillUnmount method runs during the unmounting phase of the lifecycle.</p><h4>Event Handling</h4><p>Event handling in functional components is simple and concise.</p><p>In this example, the onClick prop tells React to run the handleClick function whenever the button is clicked.</p><pre>import {useState} from &quot;react&quot;<br><br>function StateManagement() {<br>  const [color,setColor] = useState(&quot;green&quot;)<br><br>  function handleClick() {<br>    setColor(&quot;blue&quot;)<br>  }<br><br>  return(<br>    &lt;div&gt;<br>      &lt;p&gt;{color}&lt;/p&gt;<br>      &lt;button onClick={handleClick}&gt;Change the color to blue&lt;/button&gt;<br>    &lt;/div&gt;<br>  )<br>}<br><br>export default StateManagement</pre><p>The convention is to name event handlers as “handle” followed by the event name:</p><p>onSubmit={handleSubmit}</p><p>onChange={handleChange}</p><p>onKeyUp={handleKeyUp}</p><p>Class components handle events a little differently; they must ensure the event handler is bound so that the this keyword refers to the component instance rather than being undefined. Arrow functions automatically bind this<em> </em>while normal class methods do not. There are three common ways to bind event handlers in class components.</p><pre>import {Component} from &#39;react&#39;<br><br>class Binding extends Component{<br>  state = {<br>    month: &#39;November&#39;<br>  }<br><br>  handleClick() {<br>    this.setState({<br>      month: &#39;December&#39;<br>    })<br>  }<br><br>  render() {<br>    return (<br>      &lt;&gt;<br>        &lt;h1&gt;{this.state.month}&lt;/h1&gt;<br>        &lt;button onClick={this.handleClick.bind(this)}&gt;Change Month&lt;/button&gt;<br>      &lt;/&gt;<br>    )<br>  }<br>}<br><br>export default Binding</pre><p>In this first example, handleClick is a normal class method so does not automatically bind this. As a result, .bind(this)<em> </em>is used in the JSX to explicitly bind<em> </em>this<em> </em>to the handleClick<em> </em>method.</p><pre>import {Component} from &#39;react&#39;<br><br>class Binding extends Component{<br>  state = {<br>    month: &#39;November&#39;<br>  }<br><br>  handleClick = () =&gt; {<br>    this.setState({<br>      month: &#39;December&#39;<br>    })<br>  }<br><br>  render() {<br>    return (<br>      &lt;&gt;<br>        &lt;h1&gt;{this.state.month}&lt;/h1&gt;<br>        &lt;button onClick={this.handleClick}&gt;Change Month&lt;/button&gt;<br>      &lt;/&gt;<br>    )<br>  }<br>}<br><br>export default Binding</pre><p>In the second example, handleClick is defined as an arrow function. Because arrow functions automatically bind this<em>, </em>no call to .bind(this) is required. The event handler can be passed directly as this.handleClick.</p><p><strong>Note:</strong> Parentheses should not be added after this.handleClick in this format, or the method will be invoked immediately during rendering instead of when the button is clicked.</p><pre>import {Component} from &#39;react&#39;<br><br>class Binding extends Component{<br>  state = {<br>    month: &#39;November&#39;<br>  }<br><br>  handleClick() {<br>    this.setState({<br>      month: &#39;December&#39;<br>    })<br>  }<br><br>  render() {<br>    return (<br>      &lt;&gt;<br>        &lt;h1&gt;{this.state.month}&lt;/h1&gt;<br>        &lt;button onClick={() =&gt; this.handleClick()}&gt;Change Month&lt;/button&gt;<br>      &lt;/&gt;<br>    )<br>  }<br>}<br><br>export default Binding</pre><p>In the third example, handleClick is again a normal class method. However, an arrow function is created inline within the render method, and this arrow function correctly preserves this<em> </em>when calling handleClick.</p><h4>Conclusion</h4><p>According to React, functional components are recommended for most new development due to their simplicity and flexibility. However, it is important to understand class components as they are still common in older React code bases. If you are working with an existing project that uses class components you may need to continue using them.</p><h4>Sources/Additional Reading</h4><p><a href="https://www.geeksforgeeks.org/blogs/differences-between-functional-components-and-class-components/">Functional and Class Components</a></p><p><a href="https://www.w3schools.com/react/react_class.asp">Class Components</a></p><p><a href="https://www.w3schools.com/react/react_lifecycle.asp">React Lifecycle</a></p><p><a href="https://www.geeksforgeeks.org/reactjs/reactjs-useeffect-hook/">useEffect Hook</a></p><p><a href="https://www.geeksforgeeks.org/reactjs/reactjs-binding-event-handlers/">Event Handling in Class Components</a></p><p><a href="https://react.dev/learn/responding-to-events">Responding to Events</a></p><p><a href="https://www.geeksforgeeks.org/reactjs/react-events-reference/">React Events</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31243661b3f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>