<?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: Dan Zhang</title>
    <description>The latest articles on DEV Community by Dan Zhang (@daniela52588384).</description>
    <link>https://dev.to/daniela52588384</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F451135%2F38d9998d-75f7-4083-8127-7138e064aa17.jpg</url>
      <title>DEV Community: Dan Zhang</title>
      <link>https://dev.to/daniela52588384</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daniela52588384"/>
    <language>en</language>
    <item>
      <title>Context Vs Props in React</title>
      <dc:creator>Dan Zhang</dc:creator>
      <pubDate>Wed, 12 Aug 2020 22:43:01 +0000</pubDate>
      <link>https://dev.to/daniela52588384/context-vs-props-in-react-10h5</link>
      <guid>https://dev.to/daniela52588384/context-vs-props-in-react-10h5</guid>
      <description>&lt;p&gt;In this blog we will discuss:&lt;br&gt;
-What is the context?&lt;br&gt;
-Pros of using context over props&lt;br&gt;
-Cons of using context&lt;br&gt;
-How to use context?&lt;br&gt;
What is the context?&lt;br&gt;
Context is universal centralized data for your application. It allows us to pass data and methods to the Components.&lt;/p&gt;

&lt;p&gt;Pros:&lt;br&gt;
If you are using props, and you have to pass data to the last Child component amongst multiple Nested Component. The data needs to be passed through every component in the tree using props. So every component needs to know about that props data even if it's not using it.&lt;br&gt;
Context Solves this problem. You can set the context on top and all the components in the middle don’t have to know anything about it and the one down at the bottom can access it. It kind of fills the same need as redux.&lt;br&gt;
Cons:&lt;br&gt;
Context makes debugging difficult because it's difficult to figure out what caused the error when you cannot see the data in the child components that are not importing Context. Even when you are looking at the App Component and you know there is a problem in one of the properties of state, but you have to look at all the components which are Consuming it to figure out which one of them caused the problem. Use context only when you have to.&lt;br&gt;
How to use context for your React application?&lt;br&gt;
What we need to do is create a Component to create Context which will give us a Provider and a Consumer. We can wrap our Components inside of the Provider and it will provide the data passed inside of it to all the Components, which can be accessed via Consumer(s) using context.&lt;br&gt;
Create a component call DataContext.js ( You can name it whatever you like )&lt;br&gt;
// DataContext.js&lt;br&gt;
import React from "react";&lt;br&gt;
/**&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This creates two components: The Provider Component and a Consumer Component.&lt;/li&gt;
&lt;li&gt;The provider is going to make the data you pass in the Provider available everywhere underneath it and the consumer is going to read from the provider&lt;/li&gt;
&lt;li&gt;These are dummy methods. They don't do anything You are just describing to React what they look like.
*/
const DataContext = React.createContext({
name: 'John',
age: '23'
handleNameChange() {}
handleAgeChange() {}
});
export const Provider = SearchContext.Provider;
export const Consumer = SearchContext.Consumer;
Let's create three components Home.js , Login.js and Register.js. Our directory structure will be like:
-src
-component
-Home.js
-Login.js
-DataContext
App.js
And our Components hierarchy will be :
App &amp;gt; Home.js &amp;gt; Login.js , Register.js
Now we will import the Provider into App.js. We define state and event handler methods here. Then we wrap all of our components into Provider and pass the state of App as its value.
The job of Provider will then be to read the state of the App and provide it to other components. It will also make the methods defined here available to all the Components wrapped inside of the Provider.
// App.js
import React, { Component } from 'react';
import { Provider } from "./component/DataContext";
import Home from "./component/Home";
class App extends Component {
constructor( props ) {
  super( props );
  this.state = {
     name: '',
     age: '',
     handleNameChange: this.handleNameChange
     handleAgeChange: this.handleAgeChange,
  }
}
handleNameChange = ( event ) =&amp;gt; {
  this.setState({
     name: event.target.value
  });
};
handleAgeChange = ( event ) =&amp;gt; {
  this.setState({
     age: event.target.value
  });
};
render() {
return (
  
  
     
  
  
);
}
}
export default App;
The Home.js which will be the parent of Login.js and Register.js
We don’t need to pass props in Home.js because context from Provide can be used in any component. So the Child Components Login and Register can directly use them
// Home.js
import React from 'react';
import Login from "./Login";
import Register from "./Register";
export default class Home extends React.Component {
render() {
return(




);
}
}
Now in we can wrap our Login.js content into  and pass it inside context like so :
All the methods of App.js and the state will be available in context ( example context.handleNameChange, context.name etc ). So instead of using this or props, you can use context.
import React from 'react';
import {Consumer} from "./DataContext";
export default class Login extends React.Component {
render() {
  return(
     
        { context =&amp;gt; (
           
              
                 Name
                 
                 { context.name }
              
           
        ) }
     
);
}
}
We can do the same for Register.js as well.
import React from 'react';
import { Consumer } from "./DataContext";
export default class Register extends React.Component {
render() {
return(

 { context =&amp;gt; (
  
   
    Age
    
   
  
 ) }

);
}
}&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>context</category>
    </item>
    <item>
      <title>What was wrong with class components?</title>
      <dc:creator>Dan Zhang</dc:creator>
      <pubDate>Wed, 12 Aug 2020 22:36:29 +0000</pubDate>
      <link>https://dev.to/daniela52588384/what-was-wrong-with-class-components-4bi7</link>
      <guid>https://dev.to/daniela52588384/what-was-wrong-with-class-components-4bi7</guid>
      <description>&lt;p&gt;There is something beautiful and pure about the notion of a stateless component that takes some props and returns a React element. It is a pure function and as such, side effect free.&lt;/p&gt;

&lt;p&gt;export const Heading: React.FC = ({ level, className, tabIndex, children, ...rest }) =&amp;gt; {&lt;br&gt;
  const Tag = &lt;code&gt;h${level}&lt;/code&gt; as Taggable;&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;br&gt;
      {children}&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;Unfortunately, the lack of side effects makes these stateless components a bit limited, and in the end, something somewhere must manipulate state. In React, this generally meant that side effects are added to stateful class components. These class components, often called container components, execute the side effects and pass props down to these pure stateless component functions.&lt;/p&gt;

&lt;p&gt;There are several well-documented problems with the class-based lifecycle events. One of the biggest complaints is that you often have to repeat logic in componentDidMount and componentDidUpdate.&lt;/p&gt;

&lt;p&gt;async componentDidMount() {&lt;br&gt;
  const response = await get(&lt;code&gt;/users&lt;/code&gt;);&lt;br&gt;
  this.setState({ users: response.data });&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;async componentDidUpdate(prevProps) {&lt;br&gt;
  if (prevProps.resource !== this.props.resource) {&lt;br&gt;
    const response = await get(&lt;code&gt;/users&lt;/code&gt;);&lt;br&gt;
    this.setState({ users: response.data });&lt;br&gt;
  }&lt;br&gt;
};&lt;br&gt;
If you have used React for any length of time, you will have encountered this problem.&lt;/p&gt;

&lt;p&gt;With Hooks, this side effect code can be handled in one place using the effect Hook.&lt;/p&gt;

&lt;p&gt;const UsersContainer: React.FC = () =&amp;gt; {&lt;br&gt;
  const [ users, setUsers ] = useState([]);&lt;br&gt;
  const [ showDetails, setShowDetails ] = useState(false);&lt;/p&gt;

&lt;p&gt;const fetchUsers = async () =&amp;gt; {&lt;br&gt;
   const response = await get('/users');&lt;br&gt;
   setUsers(response.data);&lt;br&gt;
 };&lt;/p&gt;

&lt;p&gt;useEffect( () =&amp;gt; {&lt;br&gt;
    fetchUsers(users)&lt;br&gt;
  }, [ users ]&lt;br&gt;
 );&lt;/p&gt;

&lt;p&gt;// etc.&lt;br&gt;
The useEffect Hook is a considerable improvement, but this is a big step away from the pure stateless functions we previously had. Which brings me to my first frustration.&lt;/p&gt;

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