<?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: Asad ur Rehman</title>
    <description>The latest articles on DEV Community by Asad ur Rehman (@jinn_khan).</description>
    <link>https://dev.to/jinn_khan</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%2F149545%2F075d10b4-e8e1-42b1-a7e6-69f78bb0045a.jpg</url>
      <title>DEV Community: Asad ur Rehman</title>
      <link>https://dev.to/jinn_khan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jinn_khan"/>
    <language>en</language>
    <item>
      <title>Another React ReadMoreComponent</title>
      <dc:creator>Asad ur Rehman</dc:creator>
      <pubDate>Thu, 19 Jan 2023 15:50:09 +0000</pubDate>
      <link>https://dev.to/jinn_khan/another-react-readmorecomponent-30ga</link>
      <guid>https://dev.to/jinn_khan/another-react-readmorecomponent-30ga</guid>
      <description>&lt;p&gt;Here is another implementation for a ReadMoreComponent for React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const AnotherReadMoreComponent = ({ text, cutOff }) =&amp;gt; {
  const [showReadMore, setShowReadMore] = useState(true);
  const [displayMore, setDisplayMore] = useState(true);
  const getText = () =&amp;gt; {
    if (text?.length &amp;lt;= cutOff) {
      setDisplayMore(false);
      return text;
    } else {
      return text.slice(0, cutOff);
    }
  };
  useEffect(() =&amp;gt; {
    getText();
  }, []);
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p
        style={{
          textAlign: "justify"
        }}
      &amp;gt;
        {showReadMore ? getText() : text}

        {displayMore &amp;amp;&amp;amp; (
          &amp;lt;span
            style={{
              display: "inline",

              cursor: "pointer"
            }}
            onClick={() =&amp;gt; {
              setShowReadMore(!showReadMore);
            }}
          &amp;gt;
            {showReadMore ? "...more" : "...less"}
          &amp;lt;/span&amp;gt;
        )}
      &amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this example, the component accepts two props &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;cutOff&lt;/code&gt; which is the full text to be displayed and how much length you want to display. &lt;/p&gt;

&lt;p&gt;When the component is rendered, it will show the short text version of the content with a "read more" text to click on. When the text is clicked, the component's state is updated to show the full text and a "...less" button.&lt;/p&gt;

&lt;p&gt;This component also uses a &lt;code&gt;getText()&lt;/code&gt; method which checks if the &lt;code&gt;text&lt;/code&gt; length is less than or equal to &lt;code&gt;cutOff&lt;/code&gt; it displays the whole string without &lt;code&gt;more or less&lt;/code&gt; options. &lt;/p&gt;

&lt;p&gt;You can use this component in other component by passing the text and cutOff as prop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;AnotherReadMoreComponent

text="Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

cutOff="100"

/&amp;gt;



&amp;lt;AnotherReadMoreComponent

text="Sed ut perspiciatis unde omnis iste natus "

cutOff="10"

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

&lt;/div&gt;



&lt;p&gt;For Live Demo here is the link : &lt;a href="https://codesandbox.io/s/relaxed-babbage-3mwo1y?file=/src/App.js:1024-2080" rel="noopener noreferrer"&gt;https://codesandbox.io/s/relaxed-babbage-3mwo1y?file=/src/App.js:1024-2080&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How Babel transforms JSX to React.createElement()</title>
      <dc:creator>Asad ur Rehman</dc:creator>
      <pubDate>Mon, 02 Jan 2023 06:18:33 +0000</pubDate>
      <link>https://dev.to/jinn_khan/how-babel-transforms-jsx-to-reactcreateelement-442p</link>
      <guid>https://dev.to/jinn_khan/how-babel-transforms-jsx-to-reactcreateelement-442p</guid>
      <description>&lt;p&gt;Consider the following component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AnimalJSX = (props)=&amp;gt;{
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1&amp;gt;{props.name}&amp;lt;/h1&amp;gt;
            &amp;lt;p&amp;gt;{props.description}&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;AnimalJSX name="Dog" description="A brief description"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is JSX ? It is a syntax extension to Javascript to describe what the UI shoudl look like.. JSX produces react elements which are renderd on browser.  Babel is used to transform JSX to raw javascript. &lt;/p&gt;

&lt;p&gt;Ever wondered how this works under the hood how babel transforms this to raw javascript methods of React.&lt;/p&gt;

&lt;p&gt;React uses React.createElement() to create an elements that needs to be rendered. Here is a simplified version of the createElement function signature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React.createElement(type,props,children)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;type: the type of element you want to create  for example: h1,h2, div,span or another React.createElement() object.&lt;/p&gt;

&lt;p&gt;props: the props we want to pass into the components for example className for styling or other parameters. &lt;/p&gt;

&lt;p&gt;children: An array of text nodes or other React.createElement objects we want to render as children for this particular component. &lt;/p&gt;

&lt;p&gt;If you pass a text value as an array object it will be treated as a text node only. &lt;/p&gt;

&lt;p&gt;Consider the following code that renders the AnimalJSX  component but using createElement method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Animal = (props)=&amp;gt;{
    return React.createElement("div",{},[
        React.createElement("h1",{key:1},props.name),
        React.createElement("p",{key:2},props.description)
    ])
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we want to render the &lt;code&gt;Animal&lt;/code&gt; component in vanilla react without using JSX here is how we'll do it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const container = document.getElementById("root");

const root = ReactDOM.createRoot(container);

root.render(React.createElement(Animal,{name:'Dog',description:'A type of dog'},null));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can either omit the children parameter or just for clarity I am passing null to it. This will render as following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g4BjmGZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfr7qfekb23zs40h71ae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g4BjmGZ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfr7qfekb23zs40h71ae.png" alt="Image description" width="302" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React-17 vs React-18 Root Render Changes</title>
      <dc:creator>Asad ur Rehman</dc:creator>
      <pubDate>Mon, 02 Jan 2023 04:48:01 +0000</pubDate>
      <link>https://dev.to/jinn_khan/react-17-vs-react-18-root-render-changes-4e11</link>
      <guid>https://dev.to/jinn_khan/react-17-vs-react-18-root-render-changes-4e11</guid>
      <description>&lt;h2&gt;
  
  
  React-17 vs React-18 Root Render Changes.
&lt;/h2&gt;

&lt;p&gt;React 18 introduces a new root API which provides better ergonomics for managing roots. The new root API also enables the new concurrent renderer, which allows you to opt-into concurrent features.&lt;/p&gt;

&lt;p&gt;As react can be added to any javascript based application. I have created two vanilla html files&lt;/p&gt;

&lt;h3&gt;
  
  
  React-17.html &amp;amp; React-18.html
&lt;/h3&gt;

&lt;p&gt;For understanding purposes I am using development cdn version from ### unpkg&lt;/p&gt;

&lt;p&gt;Up to React-17 we use the ReactDOM.render() method to directly render the components by using ReactDOM.render() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
React.createElement(&amp;lt;Your App or Root Component&amp;gt;),
document.getElementById("root")
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ReactDOM takes the component and renders it to the target div on the page in this case "root" as created in &lt;code&gt;React-17.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However in React-18 ReactDOM.render api is no longer available and in order to Render a component using react we have to use the render method of createRoot() method to render Component&lt;/p&gt;

&lt;p&gt;In order to use createRoot.render method we first need to pass our target "div / container" to the createRoot method of ReactDOM as done in &lt;code&gt;React-18.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const container = document.getElementById("root");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are getting the target div by using javascript DOM method.&lt;/p&gt;

&lt;p&gt;we'll pass the target element to the createRoot() method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const root = ReactDOM.createRoot(container);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can use any variable names for root or container.&lt;/p&gt;

&lt;p&gt;This will create a root or parent container where the react elements will be rendered. In order to render our root component pass it to the render() method of root by using React.createElement()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root.render(React.createElement(&amp;lt;Your App or Root Component&amp;gt;));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These changes are already part of updated create-react-app. This is repo is just for understanding purposes on how the React.18 has changed the root rendering for the app.&lt;/p&gt;

&lt;p&gt;For a quick overview here is the difference in terms of just the raw javascript codes&lt;/p&gt;

&lt;h2&gt;
  
  
  React-17
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
      const App = () =&amp;gt; {
        return React.createElement("div", {}, [
          React.createElement(
            "h1",
            { className: "title", key: "title" },
            "React &amp;lt;App/&amp;gt; is Rendered"
          )
        ]);
      };
      ReactDOM.render(
        React.createElement(App),
        document.getElementById("root")
      );
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React-18
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script&amp;gt;
      const App = () =&amp;gt; {
        return React.createElement("div", {}, [
          React.createElement(
            "h1",
            { className: "title", key: "title" },
            "React &amp;lt;App/&amp;gt; is Rendered"
          )
        ]);
      };
      const container = document.getElementById("root");
      const root = ReactDOM.createRoot(container);
      root.render(React.createElement(App));
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete html files are listed below for reference. &lt;/p&gt;

&lt;h2&gt;
  
  
  React-17.html
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;React App is not Rendered&amp;lt;/div&amp;gt;
    &amp;lt;script
      crossorigin
      src="https://unpkg.com/react@17/umd/react.development.js"
    &amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script
      crossorigin
      src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
    &amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script&amp;gt;
      const App = () =&amp;gt; {
        return React.createElement("div", {}, [
          React.createElement(
            "h1",
            { className: "title", key: "title" },
            "React &amp;lt;App/&amp;gt; is Rendered"
          )
        ]);
      };
      ReactDOM.render(
        React.createElement(App),
        document.getElementById("root")
      );
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  React-18.html
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;React App is not Rendered&amp;lt;/div&amp;gt;
    &amp;lt;script
      crossorigin
      src="https://unpkg.com/react@18/umd/react.development.js"
    &amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script
      crossorigin
      src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
    &amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script&amp;gt;
      const App = () =&amp;gt; {
        return React.createElement("div", {}, [
          React.createElement(
            "h1",
            { className: "title", key: "title" },
            "React &amp;lt;App/&amp;gt; is Rendered"
          )
        ]);
      };
      const container = document.getElementById("root");
      const root = ReactDOM.createRoot(container);
      root.render(React.createElement(App));
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more details please check: &lt;a href="https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-client-rendering-apis" rel="noopener noreferrer"&gt;https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#updates-to-client-rendering-apis&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
