<?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: Nikhil Soni</title>
    <description>The latest articles on DEV Community by Nikhil Soni (@iamchiki).</description>
    <link>https://dev.to/iamchiki</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%2F395044%2F704cca7d-9103-417b-81d6-25373daf14d6.jpeg</url>
      <title>DEV Community: Nikhil Soni</title>
      <link>https://dev.to/iamchiki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iamchiki"/>
    <language>en</language>
    <item>
      <title>How to implement conditional rendering in react.</title>
      <dc:creator>Nikhil Soni</dc:creator>
      <pubDate>Sat, 12 Feb 2022 11:26:26 +0000</pubDate>
      <link>https://dev.to/iamchiki/how-to-implement-conditional-rendering-in-react-pc8</link>
      <guid>https://dev.to/iamchiki/how-to-implement-conditional-rendering-in-react-pc8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello everyone,&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;In this article, let us understand how to render react elements conditionally.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;React allows us to render UI markup based on different conditions. There may be more possible ways to implement conditional rendering in react, but i am going to discuss only a few points that are most commonly used.&lt;/p&gt;

&lt;p&gt;To understand conditional rendering clearly we will take a simple example. Suppose we have two buttons in our app Login and Logout. &lt;br&gt;
 Our requirement is such like when we are logged in then &lt;code&gt;Logout&lt;/code&gt; button should display and when we have logged out then &lt;code&gt;Login&lt;/code&gt; button should display.&lt;/p&gt;

&lt;p&gt;So how we will solve this problem.&lt;/p&gt;

&lt;p&gt;Let's have a look one by one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;By using &lt;code&gt;if else&lt;/code&gt; Statement.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While executing &lt;code&gt;if else&lt;/code&gt; block, when condition is satisfied then code inside &lt;code&gt;if&lt;/code&gt; block will execute otherwise code inside &lt;code&gt;else&lt;/code&gt; block will execute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const [isLoggedin, setIsLoggedin] = useState(true);

  const displayButton = () =&amp;gt; {
    if (isLoggedin) {
      return &amp;lt;button&amp;gt;Logout&amp;lt;/button&amp;gt;;
    } else {
      return &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;;
    }
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Conditional rendering&amp;lt;/h1&amp;gt;
      {displayButton()}
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we know we can write the javascript expression in jsx, so we have written one function which returns &lt;code&gt;button&lt;/code&gt; element based on which condition is true and that particular returned element only will render.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;By using Element variables.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this approach instead of using javascript function in jsx expression, we will use jsx element variable. Value of jsx element will be decided based on conditione like same as the previous approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const [isLoggedin, setIsLoggedin] = useState(true);

  let Button;

  if (isLoggedin) {
    Button = &amp;lt;button&amp;gt;Logout&amp;lt;/button&amp;gt;;
  } else {
    Button = &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;;
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Conditional rendering&amp;lt;/h1&amp;gt;
      {Button}
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we are using &lt;code&gt;Button&lt;/code&gt; variable as jsx element and its value is assigned based on &lt;code&gt;isLoggedin&lt;/code&gt; state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By Using Ternary Operators&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const [isLoggedin, setIsLoggedin] = useState(true);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Conditional rendering&amp;lt;/h1&amp;gt;
      {isLoggedin ? &amp;lt;button&amp;gt;Logout&amp;lt;/button&amp;gt; : 
      &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, instead of using conditions in component functions, we are applying conditions in jsx expression itself. For applying conditions we are using the ternary operator, as we know ternary operators use three operands, the first is the condition itself, the second is the result which is returned if the condition is satisfied and the third is returned when the condition is not satisfied.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By Using Logical &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This approach is also a little bit similar to the previous approach but in this approach instead of the ternary operator, we use logical &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const [isLoggedin, setIsLoggedin] = useState(true);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Conditional rendering&amp;lt;/h1&amp;gt;
      {isLoggedin &amp;amp;&amp;amp; &amp;lt;button&amp;gt;Logout&amp;lt;/button&amp;gt;}
      {!isLoggedin &amp;amp;&amp;amp; &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example there are two jsx expressions for rendering &lt;code&gt;button&lt;/code&gt; element, based on &lt;code&gt;isLoggedin&lt;/code&gt; state suitable jsx expression will be evaluated.&lt;/p&gt;

&lt;p&gt;There may be more approaches but above mentioned approaches are commonly used.&lt;/p&gt;

&lt;p&gt;I hope this post was useful.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Handling multiple inputs in react.</title>
      <dc:creator>Nikhil Soni</dc:creator>
      <pubDate>Fri, 11 Feb 2022 15:22:26 +0000</pubDate>
      <link>https://dev.to/iamchiki/handling-multiple-inputs-in-react-28ig</link>
      <guid>https://dev.to/iamchiki/handling-multiple-inputs-in-react-28ig</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello everyone&lt;/strong&gt;,&lt;br&gt;
&lt;em&gt;In this article, let us understand how to handle multiple inputs in react.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Recently I was working on one side project where I had multiple &lt;code&gt;input&lt;/code&gt; fields.&lt;/p&gt;

&lt;p&gt;Let's take the example of the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Form = () =&amp;gt; {
  const [name, setName] = useState('');
  const [age, setAge] = useState('');
  const [country, setCountry] = useState('');
  console.log(name, age, country);
  return (
    &amp;lt;form&amp;gt;
      &amp;lt;input
        type='text'
        value={name}
        onChange={(e) =&amp;gt; {
          setName(e.target.value);
        }}
      /&amp;gt;
      &amp;lt;input
        type='text'
        value={age}
        onChange={(e) =&amp;gt; {
          setAge(e.target.value);
        }}
      /&amp;gt;
      &amp;lt;input
        type='text'
        value={country}
        onChange={(e) =&amp;gt; {
          setCountry(e.target.value);
        }}
      /&amp;gt;
    &amp;lt;/form&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we have 3 different &lt;code&gt;input&lt;/code&gt; fields, we may have more than 3 also, and 3 different &lt;code&gt;states&lt;/code&gt; and 3 &lt;code&gt;onChange&lt;/code&gt; function to update respective states.&lt;/p&gt;

&lt;p&gt;We can solve this problem with help of the following approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AnotherForm = () =&amp;gt; {
  const [inputValues, setInputValues] = useState({
    name: '',
    age: '',
    country: '',
  });
  const inputChangeHandler = (e) =&amp;gt; {
    const { id, value } = e.target;
    setInputValues({ ...inputValues, [id]: value });
  };
  
  return (
    &amp;lt;form&amp;gt;
      &amp;lt;input
        id='name'
        type='text'
        value={inputValues.name}
        onChange={inputChangeHandler}
      /&amp;gt;
      &amp;lt;input
        id='age'
        type='text'
        value={inputValues.age}
        onChange={inputChangeHandler}
      /&amp;gt;
      &amp;lt;input
        id='country'
        type='text'
        value={inputValues.country}
        onChange={inputChangeHandler}
      /&amp;gt;
    &amp;lt;/form&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this solution, we have to manage only one &lt;code&gt;state&lt;/code&gt; which is having a &lt;code&gt;object&lt;/code&gt;, with property of the respective input field, as an initial value. All input fields will have &lt;code&gt;value&lt;/code&gt; attribute with their respective value. e.g &lt;code&gt;value={inputValues.name}&lt;/code&gt;.&lt;br&gt;
We will define one single handler function &lt;code&gt;inputChangeHandler&lt;/code&gt;, which will handle &lt;code&gt;onChange&lt;/code&gt; event for every input change.&lt;/p&gt;

&lt;p&gt;How does all this work? let us understand step by step.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we are destructuring &lt;code&gt;e.target&lt;/code&gt; object to get  &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; attributes from input fields.&lt;/li&gt;
&lt;li&gt;Then we are updating &lt;code&gt;inputValues&lt;/code&gt; state object with existing values with help of &lt;code&gt;setInputValues&lt;/code&gt; and spread operator.&lt;/li&gt;
&lt;li&gt;And now we are updating &lt;code&gt;value&lt;/code&gt; of the input field that is being triggered with &lt;code&gt;onChange&lt;/code&gt;. e.g. - &lt;code&gt;[id] : value&lt;/code&gt;, here we are setting dynamic &lt;code&gt;id&lt;/code&gt; property key.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Passing data from React Router's Link to another component.</title>
      <dc:creator>Nikhil Soni</dc:creator>
      <pubDate>Thu, 10 Feb 2022 16:28:58 +0000</pubDate>
      <link>https://dev.to/iamchiki/passing-data-from-react-routers-link-to-another-component-3boa</link>
      <guid>https://dev.to/iamchiki/passing-data-from-react-routers-link-to-another-component-3boa</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey everyone,&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;In this article, let us understand how to pass data from one route to another route.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Suppose we have a user list on the home page, all users are listed as User components.We need to go to the ViewUser component to see the detail of the user on which we clicked. &lt;/p&gt;

&lt;p&gt;How we will implement this ?&lt;/p&gt;

&lt;p&gt;There may be different approaches, but I will focus on approach in which data will be passed from one route to another route.&lt;br&gt;
To do this with React Router, we can pass data from one &lt;code&gt;Link&lt;/code&gt; to new &lt;code&gt;Route&lt;/code&gt; that is being rendered.&lt;/p&gt;

&lt;p&gt;Let's take an example to understand this approach more clearly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Home = () =&amp;gt; {
  const userList = [
    {
      id: 1,
      name: 'ajay',
      age: 25,
      country: 'India',
    },
    {
      id: 2,
      name: 'john',
      age: 35,
      country: 'US',
    },
    {
      id: 3,
      name: 'kamal',
      age: 25,
      country: 'UK',
    },
  ];
  return (
    &amp;lt;div&amp;gt;
      {userList.map((user) =&amp;gt; {
        return &amp;lt;User user={user}&amp;gt;&amp;lt;/User&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;const User = (props) =&amp;gt; {
  const { name, id } = props.user;
  return (
    &amp;lt;Link to={`/user/${id}`} state={props.user}&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;p&amp;gt;Name: {name}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;User id: {id}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Link&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, there are two components, in Home components all users are listed and another is the User component which is using &lt;code&gt;Link&lt;/code&gt; to go to another route.&lt;/p&gt;

&lt;p&gt;When the user clicks on the user component it will navigate to the ViewUser 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 ViewUser = () =&amp;gt; {
  const location = useLocation();
  const user = location.state;
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Name: {user.name}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Age: {user.age}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Country: {user.country}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned in the above ViewUser component we need user's other details name, age, country.&lt;/p&gt;

&lt;p&gt;So how we will pass this information to ViewUser Component from User Component.&lt;/p&gt;

&lt;p&gt;To do this, we have to include a &lt;code&gt;state&lt;/code&gt; prop with the data we want to pass along - in our case, it is user data.&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;Link to={`/user/${id}`} state={props.user}&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;p&amp;gt;Name: {name}&amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;User id: {id}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/Link&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So how do we get access to the data on &lt;code&gt;state&lt;/code&gt; in ViewUser? When we pass data along via the &lt;code&gt;state&lt;/code&gt; property that will be available on the location's state property, which we can get using React Router's custom hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const location = useLocation();
const user = location.state;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
