<?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>Forem: mcwachira</title>
    <description>The latest articles on Forem by mcwachira (@mcwachira).</description>
    <link>https://forem.com/mcwachira</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%2F406651%2F435590e5-2b84-4206-a082-926b201dba0a.jpeg</url>
      <title>Forem: mcwachira</title>
      <link>https://forem.com/mcwachira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mcwachira"/>
    <language>en</language>
    <item>
      <title>Day 5 of the #100daysofCode Challenge. Learning React
React Lifecycle Methods</title>
      <dc:creator>mcwachira</dc:creator>
      <pubDate>Mon, 01 Mar 2021 09:11:44 +0000</pubDate>
      <link>https://forem.com/mcwachira/day-5-of-the-100daysofcode-challenge-learning-react-react-lifecycle-methods-62c</link>
      <guid>https://forem.com/mcwachira/day-5-of-the-100daysofcode-challenge-learning-react-react-lifecycle-methods-62c</guid>
      <description>&lt;p&gt;Its Day 5 of the #100daysofCode challenge and we are going to talk about react lifecycle methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Lifecycle Methods
&lt;/h2&gt;

&lt;p&gt;So the first question that comes  to mind  is what are React lifecycle methods?&lt;br&gt;
You can think of lifecycle methods as series of events that happen sequentially from the conception and birth  of  a react component to its death.&lt;br&gt;
There are three main stages in a react component lifecycle where the component ia monitored and manipulated.&lt;br&gt;
The three main stages include&lt;br&gt;
1.Mounting        - conception and birth  of a react component&lt;br&gt;
2.updating        - growth of a react component&lt;br&gt;
3.unmounting   - death of a react component&lt;/p&gt;
&lt;h3&gt;
  
  
  Mounting
&lt;/h3&gt;

&lt;p&gt;This is the stage where elements are placed in the dom.&lt;br&gt;
There are four built in methods in this stage&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constructor()&lt;/li&gt;
&lt;li&gt;static getDerivedStateFromProps()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;componentDidMount()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;out off the four the most commonly methods are&lt;/p&gt;
&lt;h3&gt;
  
  
  1.constructor()
&lt;/h3&gt;

&lt;p&gt;The constructor() method is called before the component is initialized and its where the initial state is placed&lt;br&gt;
The method is called with props as arguments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Counter extends React.Component {
  constructor(props) {
    super(props);
      count: 0,
    };
  }

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

&lt;/div&gt;



&lt;p&gt;In the above example we have created a simple counter component and placing its initial state in the constructor method and setting its initial value to zero.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.render()
&lt;/h3&gt;

&lt;p&gt;This is the only required method in a component and its role is to handle the rendering of your component to the UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person extends React.Component{
render(){
 return    &amp;lt;div&amp;gt; my name is charles and am 27 years old&amp;lt;/div&amp;gt;      
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example shows the render() method returning jsx which is displayed on the ui.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.componentDidMount()
&lt;/h3&gt;

&lt;p&gt;This method comes into play after your component has been mounted.Its called once in the lifecyle process and it signifies that you component has been rendered properly.&lt;br&gt;
Its at this point where data from a remote api can be brought in.&lt;br&gt;
Here we can use the setSate() method to update the state.This will cause a re-render to occur  but it will happen before the browser updates the ui.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
   const json = localStorage.getItem("count");
   const count = JSON.parse(json);
   this.setState(() =&amp;gt; ({ count: count }));
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above simple example data is fetched from local storage and it used to update the state via the setState method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating
&lt;/h3&gt;

&lt;p&gt;Its the next stage in the lifecycle process and its during this stage that the component is updated.&lt;br&gt;
It consist of five built in methods which are called in order when the component is updated&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;static getDerivedStateFromProps()&lt;/li&gt;
&lt;li&gt;shouldComponentUpdate()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;getSnapshotBeforeUpdate()&lt;/li&gt;
&lt;li&gt;componentDidUpdate()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the update stage the the most common method is&lt;/p&gt;
&lt;h3&gt;
  
  
  componentDidUpdate()
&lt;/h3&gt;

&lt;p&gt;Its called after the component has been updated in the dom and any rendered html has finished loading.&lt;br&gt;
It takes in two arguments props and state which updates the dom as soon as changes occur in either.&lt;br&gt;
The setState() can be called in this method but it  must be wrapped in a conditional statement in order to check for changes in state or props. &lt;br&gt;
Wrapping it in  conditional statement will prevent it from from forming an infinite loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count) {
      const json = JSON.stringify(this.state.count);
      localStorage.setItem("count", json);
    }

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

&lt;/div&gt;



&lt;p&gt;In the above code example we taking in our previous prop and state and checking if the previous state count  is similar to the current state count and if its not we then store the current state count in local storage. &lt;/p&gt;

&lt;h3&gt;
  
  
  Unmounting
&lt;/h3&gt;

&lt;p&gt;This is the next phase of the lifecycle where the component is removed from the dom.&lt;br&gt;
It consist of only one method &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillUnmount()&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  componentWillUnmount()
&lt;/h3&gt;

&lt;p&gt;Its called write before the component is removed from the dom.&lt;br&gt;
In this method you can perform  the necessary clean ups like invalidating timers, canceling network requests, removing event listeners or canceling any subscriptions made in componentDidMount().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentWillUnmount() {
  clearInterval(this.interval);
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  REACT COMPONENT LIFECYCLE DIAGRAM
&lt;/h2&gt;

&lt;p&gt;The diagram below gives an overview of the different  react lifecycle methods. &lt;a href="%5B%20https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/"&gt;Its from the official react documentation &lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S-TodoRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1614589398825/iA9IWpOf5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S-TodoRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1614589398825/iA9IWpOf5.jpeg" alt="download (4).jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This has been my firth day learning  react and is has been awesome and interesting.&lt;br&gt;
cheers guys and happy learning.&lt;br&gt;
  &lt;a href="https://twitter.com/mc_wachira"&gt;Connect with me on twitter and lets talk about react&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>codenewbie</category>
      <category>webdev</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Day 4 of the #100daysofCode Challenge. Learning React</title>
      <dc:creator>mcwachira</dc:creator>
      <pubDate>Sun, 28 Feb 2021 12:18:53 +0000</pubDate>
      <link>https://forem.com/mcwachira/day-4-of-the-100daysofcode-challenge-learning-react-2k88</link>
      <guid>https://forem.com/mcwachira/day-4-of-the-100daysofcode-challenge-learning-react-2k88</guid>
      <description>&lt;h2&gt;
  
  
  STATEFUL OR STATELESS COMPONENTS WHICH ONE TO CHOSE AND WHY?
&lt;/h2&gt;

&lt;p&gt;During my fourth day in my #100dayofCode journey I have been learning about stateful and stateless components and I decided to write an article about it.&lt;/p&gt;

&lt;p&gt;The first question that comes to mind is what is state?&lt;br&gt;
State is simply an object that hold information about a react Component. State is used in tracking changes that occur in a react Component and re-rendering it.&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 from "react";
import ReactDOM from "react-dom";

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = { date: new Date() };
  }

  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;
        &amp;lt;h2&amp;gt;It is {this.state.date.toLocaleTimeString()}.&amp;lt;/h2&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

ReactDOM.render(&amp;lt;Clock /&amp;gt;, document.getElementById("root"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example is a simple component that has state and we use the state to render output on  the web page.&lt;/p&gt;

&lt;h3&gt;
  
  
  stateful and stateless components
&lt;/h3&gt;

&lt;p&gt;What is a stateful component ?&lt;br&gt;
Its is simply a  class component as it has a state object  initialized in the constructor and can change it own state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0,
    };
  }

ReactDOM.render(&amp;lt;Counter /&amp;gt;, document.getElementById("app"));

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

&lt;/div&gt;



&lt;p&gt;The above example is a stateful component as  the component contains a state object initialized in the constructor.&lt;/p&gt;

&lt;h4&gt;
  
  
  stateless components
&lt;/h4&gt;

&lt;p&gt;stateless components are simply functional components which have no state object.&lt;br&gt;
stateless components just take information via props and and outputs it on the web page.&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 from "react";
import ReactDOM from "react-dom";
const Header = (props) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;{props.title}&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

ReactDOM.render(&amp;lt;Header title="my App" /&amp;gt;, document.getElementById("app"));


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

&lt;/div&gt;



&lt;p&gt;This simple example shows a stateless component which receives information via the props and displays it.&lt;/p&gt;

&lt;p&gt;So I know  you are asking when to use one over the other &lt;/p&gt;

&lt;h3&gt;
  
  
  when to use stateless components
&lt;/h3&gt;

&lt;p&gt;1.When there is no interactivity  needed&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;when there is need to reuse your code&lt;/li&gt;
&lt;li&gt;when no state is required&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  when to use stateful components
&lt;/h3&gt;

&lt;p&gt;1.when you want to pass data to the component for rendering&lt;br&gt;
2.when you need interactivity in your web app&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;when taking in user data via input forms &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Advantages of  stateless components
&lt;/h3&gt;

&lt;p&gt;1 Easy to understand - Stateless components are usually JavaScript functions which are easy to understand. &lt;br&gt;
2.High performance - stateless components have high performance compared to stateful components as they don't have state and life cycle.&lt;br&gt;
3.Stateless components reduce the code base  size making  the code to become clean and optimized.&lt;/p&gt;

&lt;p&gt;This has been my fourth day learning  react and is has been awesome and interesting.&lt;br&gt;
cheers guys and happy learning.&lt;br&gt;
  &lt;a href="https://twitter.com/mc_wachira"&gt;Connect with me on twitter and lets talk about react&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>100daysofcode</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Day 2 and 3  of the #100daysofCode Challenge. Learning React</title>
      <dc:creator>mcwachira</dc:creator>
      <pubDate>Sun, 21 Feb 2021 09:35:26 +0000</pubDate>
      <link>https://forem.com/mcwachira/day-2-and-3-of-the-100daysofcode-challenge-learning-react-45k8</link>
      <guid>https://forem.com/mcwachira/day-2-and-3-of-the-100daysofcode-challenge-learning-react-45k8</guid>
      <description>&lt;p&gt;I have spend the yesterday and today learning react in my #100daysofCode challenge.&lt;br&gt;
My main focus has been  to learn  about props, state and setState.  I found it a bit difficult at first to understand the  concept of  state and setState but as all good students  do I never gave up and continued learning  and I now have a bit of technical know how on the subject matter.&lt;br&gt;
In order to explain props lets start by writing two components a parent and a child component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class School extends React.Component{
render(){

return(
&amp;lt;div&amp;gt;
&amp;lt;h1&amp;gt; This is a parent Component&amp;lt;/h1&amp;gt;
        &amp;lt;h2&amp;gt;    &amp;lt;Student/&amp;gt; &amp;lt;/h2&amp;gt;
&amp;lt;/div&amp;gt;)
   };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;lets now write our child 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 student =() =&amp;gt;{
return &amp;lt;p&amp;gt; my name is charles &amp;lt;/&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We may want to render the student multiple times in the School component and with different names and this is where props come in handy.&lt;/p&gt;

&lt;p&gt;So What are props? props are arguments that are passed to react components.You may think of props as parameters that are passed to functions in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arguments passed in a JavaScript function
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Add(one , two){
return one+ two;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Props passed as argument in react
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Student =(props) =&amp;gt;{
return &amp;lt;p&amp;gt; my name is charles &amp;lt;/&amp;gt;;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So to render the students component with different names and multiple times in the school component we just use props to pass data from the parent component (School) to the child component (Student);&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class School extends React.Component{
render(){

return(
&amp;lt;div&amp;gt;
&amp;lt;h1&amp;gt; This is a parent Component&amp;lt;/h1&amp;gt;
        &amp;lt;h2&amp;gt;    &amp;lt;Student name={'charles'}/&amp;gt; &amp;lt;/h2&amp;gt;
        &amp;lt;h2&amp;gt;    &amp;lt;Student name={"pamela"}/&amp;gt; &amp;lt;/h2&amp;gt;
        &amp;lt;h2&amp;gt;    &amp;lt;Student name={"Sheila"}/&amp;gt; &amp;lt;/h2&amp;gt;

&amp;lt;/div&amp;gt;)
   };
}

const Student =(props) =&amp;gt;{
return &amp;lt;p&amp;gt; my name is {props.name} &amp;lt;/p&amp;gt;;
};

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

&lt;/div&gt;



&lt;p&gt;This is what is rendered on the web page&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cVz77dsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1613889668689/ang-Fw40w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cVz77dsz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1613889668689/ang-Fw40w.jpeg" alt="download (2).jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we see that each child component renders its own prop data .So props are basically used to pass data from the parent component to the child component and in a unidirectional way meaning that  data flow is one way from parent component to child component only.&lt;/p&gt;

&lt;p&gt;Now lets talk about state.&lt;br&gt;
 ### What is state?&lt;/p&gt;

&lt;p&gt;State is simply an object that hold information about a react Component.&lt;br&gt;
State is used in tracking changes that occur in a react Component and re-rendering it.&lt;br&gt;
So let write a simple counter app to demonstrate the use of state in react&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 from "react";
import ReactDOM from "react-dom";

class Counter extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }
  handleAddOne() {
    this.setState((prevState) =&amp;gt; {
      //method to update the state
      return {
        count: prevState.count + 1,
      };
    });
  }
  handleMinusOne() {
    this.setState((prevState) =&amp;gt; {
      //method to update the state
      return {
        count: prevState.count - 1,
      };
    });
  }
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;Count:{this.state.count}&amp;lt;/h1&amp;gt;
        &amp;lt;button onClick={() =&amp;gt;this.handleAddOne()}&amp;gt;+1&amp;lt;/button&amp;gt;
        &amp;lt;button onClick={ () =&amp;gt;this.handleMinusOne()}&amp;gt;-1&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}


ReactDOM.render(&amp;lt;Counter/&amp;gt;, root);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a simple app which has two buttons  .One button to increase the value  of a number when pressed and the other to decrease the value of the number.&lt;/p&gt;

&lt;p&gt;Lets break down the code&lt;br&gt;
We start by writing our state in the constructor and initializing it with data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; constructor() {
    super();
    this.state = {
      count: 0,
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example we hard coded our data but you can use props to pass data to the state.&lt;/p&gt;

&lt;h3&gt;
  
  
  UPDATE THE STATE
&lt;/h3&gt;

&lt;p&gt;we then change our state and update it and this is where  setState() method comes in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; handleAddOne() {
    this.setState((prevState) =&amp;gt; {
      //method to update the state
      return {
        count: prevState.count + 1,
      };
    });
  }
  handleMinusOne() {
    this.setState((prevState) =&amp;gt; {
      //method to update the state
      return {
        count: prevState.count - 1,
      };
    });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we look at both the handleAddOne() and HandleMinusOne() methods the SetState()method is used to update the value of the initial state.&lt;br&gt;
The setState method takes in a callback function through which we can access the  prevState which holds the the previous state of the component where can modify and update the state.&lt;/p&gt;
&lt;h3&gt;
  
  
  There are some guidelines when it comes to using  setState()
&lt;/h3&gt;

&lt;p&gt;Don't modify the state directly&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; this.setState(() =&amp;gt; {
      //method to update the state
      return {
        count: this.state.count + 1
      };
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above codes work but its not the best way to modify the state as we as we are doing it directly.&lt;br&gt;
This is because setSate is an a asynchronous operation and the changes to the state may happen at a later time hence the setState may overwrite our manually mutated state.&lt;/p&gt;

&lt;p&gt;This two day have been a bit hectic as I was trying to understand state and props but I have managed to pull through and get the concepts.&lt;br&gt;
You can do it to us don't give up and happy learning.&lt;/p&gt;

&lt;h1&gt;
  
  
  100daysofCode.
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/state-and-lifecycle.html"&gt;React Docs&lt;/a&gt; &lt;br&gt;
 &lt;a href="https://github.com/uberVU/react-guide/blob/master/props-vs-state.md"&gt;Github&lt;/a&gt; &lt;br&gt;
 &lt;a href="https://stackoverflow.com/questions/37755997/why-cant-i-directly-modify-a-components-state-really"&gt;stackOverflow&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>100daysofcode</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Day 1 of the #100daysofCode Challenge. Learning React</title>
      <dc:creator>mcwachira</dc:creator>
      <pubDate>Fri, 19 Feb 2021 17:18:15 +0000</pubDate>
      <link>https://forem.com/mcwachira/day-1-of-the-100daysofcode-challenge-learning-react-21h1</link>
      <guid>https://forem.com/mcwachira/day-1-of-the-100daysofcode-challenge-learning-react-21h1</guid>
      <description>&lt;p&gt;Today is my first day of the #100daysofCode challenge  and though this journey I have committed myself to learn  react and become a front-end developer and also share what I have learn.&lt;br&gt;
So the first question I had to ask myself is  what is react? According to the react website  react is a JavaScript library for building beautiful interfaces. &lt;/p&gt;

&lt;p&gt;I also had to ask myself why use react  to build web apps instead of using plain old JavaScript and the main reason I found is that react divides your web app into smaller reusable parts called components.&lt;br&gt;
So before I go on talking about components let write a simple react program to display our name on a web page.&lt;br&gt;
 lets start with our html file&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;!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 name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;My Name App&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="app"&amp;gt;&amp;lt;/div&amp;gt;

    &amp;lt;script src="https://unpkg.com/react@17.0.1/umd/react.development.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.development.js"&amp;gt;
 https://unpkg.com/@babel/standalone/babel.min.js. &amp;lt;/script&amp;gt;
    &amp;lt;script src="scripts/app.js" type="text/babel"&amp;gt;&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;We start by writing the html element div and giving it an id of app as this is where our name will appear on the screen.&lt;br&gt;
 We then add links for react and react dom packages as we are using react to develop our app and need both react and react dom otherwise this would be just a plain JavaScript app. &lt;br&gt;
The react package  is used for writing react components while the react  dom package is used to provide dom specific methods like render() that helps in displaying our app on the web page.&lt;br&gt;
The Babel package included enables our browser to  understand jsx (JavaScript xml).&lt;br&gt;
Jsx just enable us to write html in react.&lt;/p&gt;

&lt;p&gt;JavaScript file&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(&amp;lt;h1&amp;gt;charles wachira&amp;lt;/h1&amp;gt;, document.getElementById('root'));

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

&lt;/div&gt;



&lt;p&gt;In our JavaScript file we use the render() method provided by the react dom package  to display our app on the web page.&lt;/p&gt;

&lt;p&gt;Now lets get back to react components. What is a react component?&lt;br&gt;
A react component is a basically  reusable pieces of  code which  building blocks of your react app. They take input data and return something to be displayed on the web page using a the render method .&lt;br&gt;
a simple component would look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myName =()=&amp;gt;&amp;lt;p&amp;gt; charles wachira&amp;lt;/p&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a component that takes no input but on displays my name.&lt;br&gt;
There are basically two types of components &lt;br&gt;
1.Class components&lt;br&gt;
2.Functional components.&lt;/p&gt;
&lt;h2&gt;
  
  
  CLASS COMPONENTS
&lt;/h2&gt;

&lt;p&gt;A class component is a component that is created or defined using ES6 class syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Hi extends React.Component {
    render() {
        return(&amp;lt;h1&amp;gt;my name is charles wachira &amp;lt;/h1&amp;gt;)
    }
}

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

&lt;/div&gt;



&lt;p&gt;This a simple class component which return a h1 element with my name on the web page using the render method.&lt;/p&gt;

&lt;h3&gt;
  
  
  FUNCTION COMPONENTS
&lt;/h3&gt;

&lt;p&gt;A function component is basically a JavaScript function that return a react element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myName(){
return&amp;lt;h1&amp;gt;my name is charles wachira &amp;lt;/h1&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function component returns  a h1 with my name.&lt;/p&gt;

&lt;p&gt;This has been my first day learning react and it has been a awesome one having learnt some react basics. I hope to to continue learning and become a  good front-end developer .&lt;br&gt;
cheers guys and happy learning .&lt;/p&gt;

&lt;h1&gt;
  
  
  100daysOfCode
&lt;/h1&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>100daysofcode</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
