Showing posts with label learn. Show all posts
Showing posts with label learn. Show all posts

Saturday, November 16, 2019

Destructuring in React - what is that?

Destructuring in React


Wondering what destructuring in React is?

It is taking a certain value out of the many other values stored together.

Or, go back to my favorite way of explaining things: comparison!


Imagine you have a basket full of eggs, and each egg has a name written on it. 
So when you want an egg, you just want this precise egg, not any egg from the basket.
Therefore, you take it out and sort it by alphabet. Every time you need a certain egg from the basket, you don't need to take out all the eggs one by one. You know exactly where to look to get
the egg with the right name!
Photo credit: someone from Pinterest

Destructuring is similar. 
It means taking a certain prop out of the basket of props so you can use it whenever you like.
For example, your prop (among others) is:

 name: 'Adam'


In case we are in a class based component, before destructuring, we will access this prop like this: this.props.name. And than we do the destructuring: 

const {name} = this.props;


and now we can use {name} every time we want to access this prop.

On the other hand, if we are in a functional component, our prop will look like this:

name: 'Adam'
and we access it like this: props.name

now destructuring in functional component will look like this:

const {name}  =  props;

Because we don't have problems with "this" keyword in functional components. This little "issue" is related to class based components only. 

Now if we are in our class based components where we want to pass props to our child component (and of course we have state), how do we do the destructuring then?
we do it like this:

const {name} = this.state; (because we are in the class that we refer to as "this". No worries, I will write more about "this" in one of my next posts.
I hope you managed to grasp the point here: getting a certain thing from a bunch of things without breaking a sweat.

Thanks for reading, stay tuned!

Friday, October 25, 2019

React - Introduction: how does it work?

React - Introduction: how does it work?



What is React actually? It is an open-source JavaScript library created by Facebook, used by Instagram and Netflix for building user interfaces. 

React is used to create dynamic web applications mainly single page applications (SPA).

So, what is a single-page application? That is a web application that does not require page reloading during use.  Maybe you're not aware of it, but you are most likely using this kind of applications every day. Here are some of the SPAs; Facebook, Gmail, GitHub or Google Maps.
Basically, SPAs allow the user to stay in a pleasant web space where information is presented to the user in a nice, workable and simple fashion.

Now, let's dive in!

The life force of React is components. We create components in React for different parts of our
application. For example,

in this photo, "Home" is a component, "Explore" is another component, "Notifications" is also a component, etc. And all of these components are nested in a single component such as a "navbar" component.
And here are some more examples of components: search box, footer, header, sidebar etc.  I hope you get the picture. 
These components create the JavaScript representation of the DOM (Document Object Model) that is called virtual DOM. 
(In case you don't know about the DOM, you can learn more by clicking here.)

Virtual DOM is what makes React fast. React uses the virtual DOM, renders it to the browser and
creates the actual DOM based on it. Essentially, every time we make a change to one of our components (the data or the UI state), React will note the change and update only the changed bits via virtual DOM, and then update it inside the actual DOM
in the browser and then we can see that change. This happens really quickly because of the virtual DOM.

When we make a change in the virtual DOM (in some of our components), React creates a new virtual DOM and compares it to the old one. It knows exactly where to make an update in the browser/DOM. It updated that changed part only, instead of updating the whole thing (which would of course take more time to complete). This happens every time one of our components state or data changes. It is continuously updating the DOM to reflect the change.
This is why we say that virtual DOM makes React fast.

Now that we've explained how components work, we should say something about components themselves. 

Components look like html templates. (usually using JSX - we will explain JSX in the next post).

They can be class-based or functional. Class-based components have a state that can be changed/ updated, and functional components are basically just that: a function that does something. Components can also contain JavaScript for functionality. 

Don't worry if you didn't figure out components yet, you can read more about components in this post.

Thanks for reading and  - happy coding!

Tuesday, October 22, 2019

Why React? 5 Reasons to learn/love React


Why React? 5 Reasons to learn/love React



Since its creation, you probably came across
this question over and over again.
Why do you need React for front end development?

Well, you don't absolutely need it. But here I will talk about several things I like about React, and if you don't get convinced by the end of the article, I didn't complete my mission...






1. Fast


Just like with other single page applications,
React application will not reload every time when you 
click on one of its components, and therefore is fast 
and reliable. Also, it uses Virtual DOM that makes the 
application fast. Virtual DOM  allows ReactJS to 
detect when exactly to re-render or when to ignore some pieces
of DOM, since it can detect when the data has changed. 
In addition, as compared to Angular, 
ReactJS yields better runtime performance. Just
wonderful!

2. Modular


A React app is made of parts called components.
Big or small, you can reuse them as many times
as you need, which is awesome. If you need
some code several times in different places, no need to 
rewrite or look it up and copy - you just nest your
React component inside and you're all set!
This is one of the best things about React - 
you can create bigger components out of smaller
components, use it as many time as you like, wherever
you like!


3. Simple


In React, we usually use JSX, instead of using regular JavaScript 
for templating. 
But if you don't like it, you can absolutely go for plain Javascript.
However if you do go for JSX, it's a lot of fun!
JSX is a syntax extension to JavaScript. Well, nicely put,
but what does it mean? JSX in a nutshell is HTML
intertwined with Javascript. You just need curly braces
if you want to write JavaScript expressions or
any dynamic content. Awesome, right?

4. It can be used to build mobile applications



React Native is used to build mobile applications, so
with a few adjustments, after getting the hang of React, 
you will be able to create mobile applications as well. Isn't 
that just perfect?


5. It can be tested easily


React applications are extremely easy to test.
I won't go into much detail here as my first post 
on React basics is on its way. But let's just say that you will
be able to test React components like you test any other
JavaScript code.


Of course, this list is a non-exhaustive one.

Monday, October 21, 2019

Why React for dummies?

If you're reading this article, don't feel offended by the title.
One is always a dummy before getting familiarized with a certain subject. On the other hand, if you can explain a concept that a dummy can understand, then you most likely understand it well too. (Feynman technique, you can look it up if you like).

Truth be told, on my path to learn React, those dummy articles were the most useful ones. It is those articles that, after days of searching for an answer, browsing Stackoverflow terrified that asking a question there will disable me to ask anything for the next month or two and getting just an explanation filled with complex technical terms, I come a cross a SIMPLE article. And it just 'clicked'! Suddenly I have no problems understanding the point and it basically made me so happy I wanted to share that with as many people I can. Therefore I decided to start this blog. I appreciate any comments or questions because like everyone else, I surely make mistakes. Don't be afraid to say it, I am not afraid of mistakes. I like them because they make me learn. In my next post I will write about why choose React at all. What are the features that makes it attractive, and when you can do without.

Thanks for reading!

Props: how do we pass them?

Props, how do we pass them? Hey people, it's me again. This time I am trying to share some tips on passing the props in React. Last ...