<?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: Akash Sheikh</title>
    <description>The latest articles on DEV Community by Akash Sheikh (@akash_cse).</description>
    <link>https://dev.to/akash_cse</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%2F1115690%2F7d8a1904-f62c-4821-b01f-8ae937e87a19.jpeg</url>
      <title>DEV Community: Akash Sheikh</title>
      <link>https://dev.to/akash_cse</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akash_cse"/>
    <language>en</language>
    <item>
      <title>Best Practice for React</title>
      <dc:creator>Akash Sheikh</dc:creator>
      <pubDate>Wed, 19 Jul 2023 19:58:25 +0000</pubDate>
      <link>https://dev.to/akash_cse/best-practice-for-react-2159</link>
      <guid>https://dev.to/akash_cse/best-practice-for-react-2159</guid>
      <description>&lt;p&gt;React is one of the most popular techs when talking about JavaScript. Even for some developers, it’s the best. React doesn’t force you how to structure your project. It’s completely up to you to choose how do it. As a result, that makes some developers bring the artistic sense out of them. But, on the other hand… others can make a mess. When working as a team, it’s better to make an understandable structure and code.&lt;/p&gt;

&lt;p&gt;In this article you’ll explore 18 tips about how to code better in React.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  1 — Avoid local state as much as possible
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
In case you have some calculations, avoid putting the result in a local state and rendering that state later in the JSX. Instead, move your calculations in the JSX, or create a function that returns the result and put it in the JSX.&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, { useEffect, useState } from 'react';

const App: React.FC = () =&amp;gt; {
  // ❌ Avoid: Unnecessary state
  const [result, setResult] = useState();

  // Considering a and b are two values coming from some API.
  const { a, b } = apiCall();
  // ❌ Avoid: Unnecessary useEffect
  useEffect(() =&amp;gt; {
    setResult(a + b);
  }, [a, b]);

  return (
    &amp;lt;div&amp;gt;
      {result}
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Do this&lt;/strong&gt;&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, { useEffect, useState } from 'react';

const App: React.FC = () =&amp;gt; {

  // Considering a and b are two values coming from some API.
  const { a, b } = apiCall();
  // ✅ Good: You can move it into the JSX
  return (
    &amp;lt;div&amp;gt;
      {a + b}
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  2 — Use Functional components instead of Class components
&lt;/h2&gt;

&lt;p&gt;**&lt;br&gt;
To understand the use of Functional components, we should first get to know the difference between Functional and Class component.&lt;/p&gt;

&lt;p&gt;Function component is just a plain JavaScript function which accepts props and returns a React element. On the other hand, a Class component is a class that extends from React.Component and creates a Render function that returns a React element. Obviously, this requires more code to write. With React Hooks, now you can write your components as functions instead of classes.&lt;/p&gt;

&lt;p&gt;One of the major reasons to drop Class components, in Function components there is no this binding. Besides, they are easy to read, test and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Class component&lt;/strong&gt;&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';
// ❌ Avoid: Too much code
export default class LoadData extends Component {
  constructor(props) {
    super(props);
    this.state = {
      usersData: [],
    };
  }

  componentDidMount() {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then((res) =&amp;gt; res.json())
      .then((users) =&amp;gt; {
        this.setState({
          data: users,
        });
      });
  }

  render() {
    return (
      &amp;lt;ul&amp;gt;
        { this.state.usersData.map(user =&amp;gt; &amp;lt;li key={user.id}&amp;gt;{user.name}&amp;lt;/li&amp;gt;) }
      &amp;lt;/ul&amp;gt;
    )
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The same thing with Functional component&lt;/strong&gt;&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, { useEffect, useState } from 'react';
// ✅ Good: Less code
export const LoadData: React.FC = () =&amp;gt; {

  const [usersData, setUsersData] = useState([]);

  useEffect(() =&amp;gt; {
    fetch("https://jsonplaceholder.typicode.com/users")
      .then((res) =&amp;gt; res.json())
      .then((users) =&amp;gt; {
        setUsersData(users);
      });
  }, []);

  return (
    &amp;lt;ul&amp;gt;
      {usersData.map(user =&amp;gt; &amp;lt;li key={user.id}&amp;gt;{user.name}&amp;lt;/li&amp;gt;)}
    &amp;lt;/ul&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>React — StateFull and StateLess</title>
      <dc:creator>Akash Sheikh</dc:creator>
      <pubDate>Tue, 11 Jul 2023 14:42:47 +0000</pubDate>
      <link>https://dev.to/akash_cse/react-statefull-and-stateless-5h54</link>
      <guid>https://dev.to/akash_cse/react-statefull-and-stateless-5h54</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stateful component:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A component that manages the state in class-based with state or functional with useState.&lt;/li&gt;
&lt;li&gt;In some component, the data keeps changing, for example, watching the cricket score etc.&lt;/li&gt;
&lt;li&gt;In most of the cases, the class-based components extend react component.&lt;/li&gt;
&lt;li&gt;Stateful components can use react life cycle hooks&lt;/li&gt;
&lt;li&gt;In stateful components it good to use the this instance&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Stateless component:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A component that has no internal state management in it.&lt;/li&gt;
&lt;li&gt;In some component, the data remains the same, for example, showing the static data.&lt;/li&gt;
&lt;li&gt;Function components are simply functions that receive the props and return the JSX code.&lt;/li&gt;
&lt;li&gt;Stateless components can not use the react life cycle hooks&lt;/li&gt;
&lt;li&gt;Here need not to use this instance, they just receive the props as an argument&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
