Pratham Profile picture
12 Jun, 17 tweets, 5 min read
React Hooks are so powerful and especially some additional hooks.

Do you know there is an alternative to useState called useReducer hook? It is used to manage more complex states.

Let's look into detail 🧵👇🏻
Usually, the useState hook is used to tackle states in React where you can pass the initial state and React preserves state for you between re-renders

What if you need to manage more complex states? Here useReducers comes into play
Similar to useState, the useReducer hook also returns a pair of values. Let's see what these two things are
The first value is `undefined` which is the current state (it is undefined because we didn't pass any initial state in the useReducer hook)

The second value is a dispatch function using which we can update our state.
The useReducer hook accepts 3 parameter

1. reducer
2. Initial state
3. init
Now we know that useReducer takes three parameters and return two values

Here is the complete syntax 👇🏻
Let's try to see this into action.

I just implemented a basic code here. Basically, I am calling dispatch function on button click which will eventually call reducer function function reducer() {   console.log("Inside reducer func
So far we haven't seen how the state can be managed using this.

Before that one thing to note here is that useReducer is for handling more complex states hence it is recommended to passing the current state as an object
Now we can simply update our state by passing the state parameter in the reducer function.

Something like this 👇🏻
Now every time I click on the button my state will be incremented by 1 because the reducer function returning the new state by adding one to the previous state.

Play around with it here: codesandbox.io/s/usereducer-z…
The reducer function accepts the second argument as well known as "action" using which we can tell the reducer function that what operations need to perform on the state.
Generally, we can pass different actions through dispatch to reducer via an object.
This is how you can pass different types of operations in the reducer function using the action parameter.
What if I want to pass some values through dispatch to the reducer function?

Here is "payload" comes into play. It is used to pass the value which represents the payloads of the action.

For example, suppose I want to pass "temp" variable 👇🏻

codesandbox.io/s/usereducer-z…
This may sound a little confusing for the first time. Don't worry I created a notes app for you using the useReducer hook.

Check out the code and try to play around with it for better understanding.

github.com/PrathamKumar14…
I have a video tutorial on YouTube explaining about useReducer in more bit details.

Check it out:
That's pretty much it for this thread. If you like this thread, share it with your connections it means a lot to me 💖

Peace out 😉

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Pratham

Pratham Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Prathkum

14 Jun
I randomly started Twitter but built ~100K audience in about 12 months

Here's how you can also do it 🧵👇🏻
📌 1. Make your place in the well-established tech community

This sounds tough but actually, it is not. You just need to put content but try to deliver the content from the one technology.

For example, When I started, I posted only CSS arts nothing else
Why?

Obviously, you can put diverse content as well but we need to tell the community that, hey, I am X and I am good at Y technology.

This will bring quick light to your profile.
Read 20 tweets
13 Jun
5 Great GitHub repositories for web developers

🧵👇🏻
1. HTML best practice

For writing maintainable and scalable HTML documents

🔗 github.com/hail2u/html-be… Image
2. Awesome CSS Learning

A tiny list limited to the best CSS Learning Resources

🔗 github.com/micromata/awes… Image
Read 6 tweets
13 Jun
We can create simple photoshop using CSS. Let's see how 😄

🧵👇🏻
CSS filter functions provide us the flexibility of adding graphical effects to an element. They are

- blur()
- brightness()
- contrast()
- drop-shadow()
- hue-rotate()
- grayscale()
- invert()
- opacity()
- sepia()
- saturate()
1. blur

As the term suggests, blur function simply blurs your input image. The blur that is being applied to image is known and Gaussian blur
Read 14 tweets
11 Jun
You can create your text portrait using CSS in 5 simple steps. Let's see how

🧵👇🏻
Step 1: Setting up the HTML

Create a document with a lot of lorem ipsum text so that your webpage is covered entirely by the text
Step2: Set dark background-color

A dark background will make your portrait more appealing
Read 8 tweets
11 Jun
5 beginner friendly websites to learn JavaScript

🧵👇🏻
1. JavaScript Tutorial

- This JavaScript Tutorial helps you learn JavaScript programming language from scratch quickly and effectively.

🔗 javascripttutorial.net
2. Learn JavaScript

- Interactive JavaScript tutorial.

🔗 learn-js.org/en/
Read 6 tweets
10 Jun
Introduction to CSS 🎨

CSS is an amazing and unique language that servers a great purpose. We can make our website visually good using CSS. It describes the presentation of web pages, including typography, layouts, color, etc

Let's break it down 🧵👇🏻
CSS is a stylesheet language that serves one great purpose. It makes your website visually good

Consider this amazon website with and without CSS 👇🏻
I hope you got it why CSS is important for web development. Alright let's move onto actually discussion that how you start learning it
Read 23 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(