Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

Saturday, November 16, 2019

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 time I wrote about props and what they are (more precisely here).

Now I want to write about passing them (or sending them). Let's dive in!


Props can be passed only from parent to child component, so it is all about the vertical flow.
Now I hope by now you realized how much I like comparisons...Comparisons make things easy to grasp, that is why I use them so much..

Imagine this like passing genetic information from mother to a child. Child cannot pass her/his mother any genetic information to their mum (or say just 'parent' to be politically correct). A child cannot pass genetic information to his sister or brother, either. It's always vertical - just like props!


We have talked about functional and class based components. So let's put the story into perspective.

First, we make a functional (child) component, for example. We destructure the name of those props and set it equal to this.props. You can learn more about destructuring here.


In this way, props have no value but are destructured so they can receive value from the parent component. Then, we can render props within the functional/ child component in the form we prefer to do that. Afterwards, in the parent component we import the functional component, set the state containing the value of the props we want to pass to the child (our functional) component. 

We destructure the this.state (we set the {name} equal to this.state and in the render, we insert our functional / child component and assign the props from this.state to the component, and render it. The next step is for you to open the Visual Studio Code, or any other text editor you may be using for writing your code, and try to do this thing I just talked about. We can talk, read and write all we want, but until we do it ourselves, we will never learn!


Also, for more details, look here: React documentation.

Thanks for reading and stay tuned!

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!

Wednesday, October 30, 2019

Introducing Redux into an application - Redux for dummies

First things first, why use Redux? I mean, there is Context.js of course, and there are
props that we can pass among components, right?


Well, not quite. Passing props in React is usually done vertically (from parent to
child and vice versa), but not horizontally.
photo credit: https://techblog.appnexus.com/five-tips-for-working-with-redux-in-large-applications-89452af4fdcb


On the other hand, using context can make it difficult to reuse components
(which is the golden feature of React, remember?).
When we have a large application, managing state can get tedious and complex.
That is where Redux jumps in. Redux allows the state to be stored in a single file,
from which all of the components can use it, no matter where they are in the component tree.
It does sound complex, but when you think about it, it actually makes things easier for us.


When we want to introduce Redux into our application, we should know
that there are three building blocks to it:
  • State
  • Action
  • Reducers


So, imagine state as a highly guarded treasure trove. No one can access this
precious trove with golden jewelry and gems aside from the guards who never
sleep or eat. They are the only ones to have the key and they always have
their eyes on the trove, protecting everyone else to get hold of it. The guards
are in our case reducers. Only they can actually make the change in the state
(rearrange the jewelry and gems), and no one else. 


But our guards unfortunately do not have a sense of beauty/esthetics and
no idea how to organize the treasure.  The only one who knows how to organize
the treasure is the commander. He has precise instructions how and what to do,
but he has no time to do it himself. He conveys the detailed instruction to the guards
and they complete the task then.

The commander in out story is the action. The action contains information that is
sent to the reducer to change the state. Actions are essentially Javascript
objects that must the type property and optionally it contains payload (the
part of information that should be worked on by the action). Payload can
be any valid JS data type.

So let's get back to our treasure story. When the commander (action)
sends the instructions to the guards (reducers) he is sending usually
a piece of information that is twofold: first, what exactly should they do,
how should they organize the treasure, or what type of action they are
required to undertake (how should the reducers change the state), and
second thing: information which part of the jewelry should be rearranged
(because every piece has its own stamp and code on it). Sometimes
we don't have this information if the commander asks the guards to
organize the whole treasure trove. This is some type of a "coded message".

Now let's go back to the computing world. Action type (in our
coded message) is actually the instruction on
what we want to do, and the action payload is the information that
will help us go there. For example if the action type is "LOGIN",
payload is username: "user" and password : "738292".
So these user information will help us log in, that is, enable the action we want to
complete. The Javascript object will look like this:

const someAction = {

type: 'LOGIN',
payload: {
username: 'Adam',
password: 'Adam123'
}

Sending the requested action goes through dispatch() method. So we can
consider this dispatch method as a messenger between our commander and
guards (Action and Reducers).

Okay folks, that's it for this time. The next time I will talk about examples on how to use it in code more precisely.

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 ...