<?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>DEV Community: DevM</title>
    <description>The latest articles on DEV Community by DevM (@devmariam).</description>
    <link>https://dev.to/devmariam</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%2F1616879%2F7aed13ec-0f71-4a2d-8693-727382403518.jpg</url>
      <title>DEV Community: DevM</title>
      <link>https://dev.to/devmariam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmariam"/>
    <language>en</language>
    <item>
      <title>MAPPED TYPES IN TYPESCRIPT</title>
      <dc:creator>DevM</dc:creator>
      <pubDate>Fri, 19 Jul 2024 17:16:59 +0000</pubDate>
      <link>https://dev.to/devmariam/mapped-types-in-typescript-4gpo</link>
      <guid>https://dev.to/devmariam/mapped-types-in-typescript-4gpo</guid>
      <description>&lt;p&gt;Mapped type in typescript simply allow us create a new type by iterating over a list of properties.&lt;br&gt;
Inother words, it allows you to take an existing type and transform each of its properties in some way to create a new type.&lt;/p&gt;
&lt;h2&gt;
  
  
  CONCEPT IN TYPESCRIPT
&lt;/h2&gt;

&lt;p&gt;Readonly: It makes all the properties of an object type in typescript readonly. This means once a value is assigned to a property, it can not be changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User = {
  name: string;
  age: number;
};

const user: Readonly&amp;lt;User&amp;gt; = {
  name: "Mariam",
  age: 30,
};

// user.age = 31; // Error: Cannot assign to 'age' because it is a read-only property.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generic Type: It allows you create reusable components that work with variety of types rather than a single one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function identity&amp;lt;T&amp;gt;(arg: T): T {
  return arg;
}

let stringIdentity = identity&amp;lt;string&amp;gt;("hello");
let numberIdentity = identity&amp;lt;number&amp;gt;(42);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Record: The record utility type constructs an object type whose property keys are keys and whose values are type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type RecordType = Record&amp;lt;string, number&amp;gt;;

const myRecord: RecordType = {
  apples: 10,
  oranges: 20,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pick: It allows you create a new type by picking a set of properties from an existing types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User = {
  name: string;
  age: number;
  email: string;
};

type UserPreview = Pick&amp;lt;User, "name" | "email"&amp;gt;;

const user: UserPreview = {
  name: "Mariam",
  email: "mariam@example.com",
  // age: 30, // Error: Property 'age' does not exist on type 'UserPreview'.
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Index Signature: I.S allow to define the type of values that can be assessed with an index. It's useful when the exact properties of an object type are not known in advance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type StringArray = {

};

const myArray: StringArray = ["hello", "world"];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Optional: Optional properties are properties that may or may not be present in an object. You define them with "?".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type User = {
  name: string;
  age?: number; // age is optional
};

const user1: User = { name: "Mariam" };
const user2: User = { name: "Ade", age: 25 };

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

&lt;/div&gt;



&lt;p&gt;Intersection: Intersection types combine multiple types into one. It uses the &amp;amp; symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Person = {
  name: string;
};

type Employee = {
  employeeId: number;
};

type EmployeeDetails = Person &amp;amp; Employee;

const employee: EmployeeDetails = {
  name: "Mariam",
  employeeId: 1234,
};

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

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>github</category>
      <category>development</category>
      <category>frontend</category>
    </item>
    <item>
      <title>STATE MANAGEMENT IN REACT</title>
      <dc:creator>DevM</dc:creator>
      <pubDate>Mon, 24 Jun 2024 23:12:10 +0000</pubDate>
      <link>https://dev.to/devmariam/state-management-in-react-488g</link>
      <guid>https://dev.to/devmariam/state-management-in-react-488g</guid>
      <description>&lt;p&gt;&lt;strong&gt;STATE MANAGEMENT IN REACT&lt;/strong&gt;&lt;br&gt;
WHAT DO YOU UNDERSTAND BY STATE MANAGEMENT?&lt;/p&gt;

&lt;p&gt;State is like a box that holds data. For example, you have a box where various books are kept. In this scenerio, the book box holds information that your components needs. Basically, state help a component remember the various books stored in the box. When you want to update the books in the box, there are steps involved and that is basically creating a new book or make a copy of the existing one and then set the state to use the new copy of the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [newTodo, setNewTodo] = useState(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet means that newTodo holds the current state value and the setNewTodo is the function used to update the state variable data. useState(0)means that data start with a value 0.&lt;/p&gt;

&lt;p&gt;State management is all about managing the data which influences the behaviour and rendering of your components. state can change over the lifecyle of a components, and each components of its own state and it could be updated within its component. UseState and setState are used to change a state of a components.&lt;/p&gt;

&lt;p&gt;This brings use to explaning what a component is all about because we cant talk about state change without talking about components.&lt;/p&gt;

&lt;p&gt;Let's dive right in......&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Components&lt;/strong&gt;&lt;br&gt;
Components are the building blocks of the user interface (UI). They are reusable pieces of code that present parts of the UI. Components can be as simple as a button and as complex as an entire page. Each components can manage its own state and properties which we all know as props. &lt;/p&gt;

&lt;p&gt;Props are a way of passing data from a parent to a child components. When a prop is passed down to a child component, it can not be modified but rather read the property and use it.&lt;br&gt;
For example, think of a component  as a form and props like a specific details you fill into the form. Or think of a component like a toy car, for the car to move, you need a battery (props).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LOCAL STATE AND GLOBAL STATE&lt;/strong&gt;&lt;br&gt;
These are important concepts for building dynamic and interactive user interfaces.&lt;/p&gt;

&lt;p&gt;LOCAL STATE&lt;br&gt;
Local state simply refers to state that is managed within a single components, this state is temporarily used to manage data or user interface states that does not require you to share with other components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useState} from 'react'
function Counter(){
&amp;lt;!-- useState hook initializes the local state  variable count  to 0 --&amp;gt;
const [count , setCount] = useState(0);
return(
&amp;lt;!-- in this return, it returns HTML Syntax --&amp;gt;
&amp;lt;div&amp;gt;
&amp;lt;p&amp;gt;You Clicked {count} times&amp;lt;/p&amp;gt;
&amp;lt;button onClick={()=&amp;gt; setCount(count + 1)}&amp;gt; Click me&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
);
}
export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation of the above code&lt;/p&gt;

&lt;p&gt;useState(0): this help us initialize the local state variable count with a value of 0 (the count starts from a default value of 0).&lt;/p&gt;

&lt;p&gt;state variable: count holds the current state, and setCount is the function used to update the state.&lt;/p&gt;

&lt;p&gt;updating a state: when the button is clicked, setCount(count + 1)updates the count state, which triggers a re-render of the component with the new state value.&lt;/p&gt;

&lt;p&gt;GLOBAL STATE&lt;/p&gt;

&lt;p&gt;Global state is used when you want to share state across multiple components. This can be used using context API, Redux, or Recoil.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, createContext, useContext } from 'react';
// Create a Context for the global state
const CountContext = createContext();
function CounterProvider({ children }) {
  const [count, setCount] = useState(0);
  return (
    &amp;lt;CountContext.Provider value={{ count, setCount }}&amp;gt;
      {children}
    &amp;lt;/CountContext.Provider&amp;gt;
  );
}
function CounterDisplay() {
  const { count } = useContext(CountContext);
  return &amp;lt;p&amp;gt;Global count is {count}&amp;lt;/p&amp;gt;;
}
function CounterButton() {
  const { setCount } = useContext(CountContext);
  return (
    &amp;lt;button onClick={() =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1)}&amp;gt;
      Increase Global Count
    &amp;lt;/button&amp;gt;
  );
}
function App() {
  return (
    &amp;lt;CounterProvider&amp;gt;
      &amp;lt;CounterDisplay /&amp;gt;
      &amp;lt;CounterButton /&amp;gt;
    &amp;lt;/CounterProvider&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation to the above code&lt;/p&gt;

&lt;p&gt;createContext() create a context object which holds the global state.&lt;/p&gt;

&lt;p&gt;CounterProvider wraps its children in CountContext.Provider, providing the count state and setCount function  to all nested components.&lt;/p&gt;

&lt;p&gt;CounterDisplay and CounterButton use useContext(CountContext) to access global state. CounterDisplay reads the count, and CounterButton updates the count.&lt;/p&gt;

&lt;p&gt;Context API: it is a built-in React API for managing global state without prop drilling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;FUNCTIONAL COMPONENTS&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This is a javascript fuctions, it accepts a single object argument and returns a react element &lt;br&gt;
(JSX code to render in the DOM tree). In functional components, we do not have this.state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function function_name(pass the argument name)
{
pass the function_body
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {useState} from 'react'

 const functionalComponent = ()=&amp;gt; {

 const [count, setCount] = useState(0);

 const increase = () =&amp;gt; {

     setCount(count + 1);
     }

     return (

     &amp;lt;div&amp;gt;

     &amp;lt;h1&amp;gt;{count}&amp;lt;/h1&amp;gt;

     &amp;lt;button onClick = {increase}&amp;gt;+5&amp;lt;/button&amp;gt;

     &amp;lt;/div&amp;gt;

     )

     }

 export default functionalComponent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;EXPLANATION OF THIS CODE ABOVE&lt;br&gt;
 The provided React functional component imports React and the useState hook, defines a component named functionalComponent that uses useState to manage a count state initialized to default state 0, includes an increase function that increments count by 1, returns JSX to display the current count in an h1 element and a button labeled "+5" that calls increase when clicked, and exports the component; however, the button label suggests it should increase by 5, so the increase function should be adjusted accordingly to increment by 5 instead of 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CLASS COMPONENTS&lt;/strong&gt;&lt;br&gt;
It extends React.component and include a render method to return JSX, it also manages a state using this.state and update state using this.setState and leverage on lifecycle method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LIFECYCLE METHODS IN CLASS COMPONENTS&lt;/strong&gt;&lt;br&gt;
 Lifecycle methods in class components provide hooks into the component's lifecycle, allowing you to run code at specific times during the component's life. This includes mounting, updating, and unmounting phases, as well as error handling. These methods are useful for managing side effects, optimizing performance, and ensuring proper cleanup.&lt;/p&gt;

&lt;p&gt;Breakdown of Lifecycle Methods&lt;/p&gt;

&lt;p&gt;Mounting Phase&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;constructor(props)&lt;br&gt;
Initializes the component state.&lt;br&gt;
Binds methods to the component instance.&lt;br&gt;
Called once when the component is created.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;static getDerivedStateFromProps(props, state)&lt;br&gt;
Syncs the state to the props when the component receives new props.&lt;br&gt;
Can return an object to update the state, or null if no state update is needed.&lt;br&gt;
Called during both the mounting and updating phases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;componentDidMount()&lt;br&gt;
Invoked immediately after the component is mounted.&lt;br&gt;
Used for side effects like fetching data or initializing third-party libraries.&lt;br&gt;
Called once after the initial render.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Updating Phase&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;shouldComponentUpdate(nextProps, nextState)&lt;br&gt;
Determines whether the component should re-render in response to state or prop changes.&lt;br&gt;
Returns true (default) to re-render, or false to prevent re-rendering.&lt;br&gt;
Useful for performance optimization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;getSnapshotBeforeUpdate(prevProps, prevState)&lt;br&gt;
Captures information from the DOM before it is updated.&lt;br&gt;
The value returned is passed to componentDidUpdate.&lt;br&gt;
Used for tasks like saving the scroll position before an update.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;componentDidUpdate(prevProps, prevState, snapshot)&lt;br&gt;
Invoked immediately after the component updates.&lt;br&gt;
Receives previous props, state, and the snapshot from &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;getSnapshotBeforeUpdate.&lt;br&gt;
Used for performing operations that depend on the DOM being updated.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unmounting Phase&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillUnmount()
Invoked immediately before the component is unmounted and destroyed.
Used for cleanup tasks like cancelling network requests or removing event listeners.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Error Handling&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;static getDerivedStateFromError(error):&lt;br&gt;
Used to update the state so the next render shows an error boundary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;componentDidCatch(error, info)&lt;br&gt;
Used to log error information and display a fallback UI in case of an error.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuv856qcbic1vp3omyqy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuv856qcbic1vp3omyqy.jpg" alt="Lifecycle of React Components" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of a code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';

class LifecycleDemo extends Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
    console.log('Constructor: Component is being initialized');
  }

  static getDerivedStateFromProps(props, state) {
    console.log('getDerivedStateFromProps: Sync state to props changes');
    return null; // Return null to indicate no change to state
  }

  componentDidMount() {
    console.log('componentDidMount: Component has been mounted');
  }

  shouldComponentUpdate(nextProps, nextState) {
    console.log('shouldComponentUpdate: Determine if re-render is necessary');
    return true; // Return true to proceed with rendering
  }

  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log('getSnapshotBeforeUpdate: Capture current state before update');
    return null; // Return a value to be passed to componentDidUpdate
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log('componentDidUpdate: Component was re-rendered');
  }

  componentWillUnmount() {
    console.log('componentWillUnmount: Component is being unmounted');
  }

  increaseCount = () =&amp;gt; {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    console.log('Render: Component is rendering');
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;{this.state.count}&amp;lt;/h1&amp;gt;
        &amp;lt;button onClick={this.increaseCount}&amp;gt;Increase&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default LifecycleDemo;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanaton to above code&lt;br&gt;
import react, {component} from 'react': react is the main react library while component is the base class for creating react class components.&lt;/p&gt;

&lt;p&gt;LifecycleDemo: this is the name of the class component extending component.&lt;/p&gt;

&lt;p&gt;constructor(props): initialize the components with props.&lt;/p&gt;

&lt;p&gt;super(props): calls the parent class constructor with props.&lt;/p&gt;

&lt;p&gt;this.state: set the initial state with count set to 0.&lt;/p&gt;

&lt;p&gt;getDerivedStateFromProps: a static method used to update the state based on the props changes.&lt;/p&gt;

&lt;p&gt;return null: indicate no changes to the state.&lt;/p&gt;

&lt;p&gt;componentDidMount: called after the component is added to the DOM.&lt;/p&gt;

&lt;p&gt;shouldComponentUpdate: determines if a re-render is necessary.&lt;/p&gt;

&lt;p&gt;nextProps, nextState: new props, new state.&lt;/p&gt;

&lt;p&gt;return true: indicate the component should re-render.&lt;/p&gt;

&lt;p&gt;getSnapshotBeforeUpdate: this is called right before DOM updates (captures state before DOM updates).&lt;/p&gt;

&lt;p&gt;preProps, prevState: the previous props and state.&lt;/p&gt;

&lt;p&gt;return null: indicate no snapshop value to be passed.&lt;/p&gt;

&lt;p&gt;componentDidUpdate: called after the component updates. I.e executes code after the component updates.&lt;/p&gt;

&lt;p&gt;snapshot: value returned from &lt;code&gt;getSnapshotBeforeUpdate.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;componentWillUnmount: called right before the component is removed from the DOM. It cleans up before the component unmounts.&lt;/p&gt;

&lt;p&gt;this.setState: updates the state with the new count value.&lt;/p&gt;

&lt;p&gt;render: it is a required method to returning JSX to define the component's UI. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;DIFFERENCE BETWEEN FUNCTIONAL COMPONENTS AND CLASS COMPONENTS&lt;/strong&gt;&lt;br&gt;
Functional components uses hooks (useState, useeffect) to manage the state and side effects while class component uses the this syntax and lifecycle methods for state and behavior management.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwihiy6sptaw4garb3w0c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwihiy6sptaw4garb3w0c.jpg" alt="Difference between Class Components and Function Components." width="800" height="984"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;image source:geeks4geeks&lt;/p&gt;

</description>
      <category>programming</category>
      <category>react</category>
      <category>state</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
