Same company new universe

You might know us as "enjoy.ing" - now, we call ourselves CREATEQ.
Our culture, projects, clients and all of our people, of course, remain the same.
Find out more!

New Website

    Please attach your CV (PDF or DOC, up to 5MB)

    captcha
    enjoy.ing
    • Home
    • About us
      • Who we are
    • What we do
      • Projects
      • Technologies
    • work’n’enjoy
      • Why we enjoy engineering
      • People Development – Investing in people
    • Open positions
    • News
    • Contact
    • Home
    • About us
      • Who we are
    • What we do
      • Projects
      • Technologies
    • work’n’enjoy
      • Why we enjoy engineering
      • People Development – Investing in people
    • Open positions
    • News
    • Contact

    Basic overview on React Hooks

    Home » News » Basic overview on React Hooks
    Basic overview on React Hooks
    • July, 2020
    • Share It

    If you ran into this blog post you are probably already familiar with React, one of the most popular JavaScript libraries for creating interactive UIs. It is initially released in 2013th by development team of Facebook. In case you missed it, a lot of documentation is available on the official web site.

    Components are heart of React, and they can be either Function or Class components. Both have some additional features, such as state. State is fundamental feature of React and an attribute of each component. When state changes, component re-renders.

    Hooks

    React 16.8 introduced something called hooks which allow us to use state and other features without having to use a class component. Everyone who has used React before knows that if you want to use state in your component you have to use class. That’s changing with hooks.

    The list of built-in hooks is long and they are split into:
    – Basic: useState, useEffect, useContext
    – Additional: useReducer, useCallback, useMemo, useRef, useImperativeHandle, etc

    There is even possibility of writing custom hooks. More about them you will find in the upcoming posts.

    Stateful class component vs functional component

    We can now have state within functional base component with useState hook. In the following example you will see how to replace class state with useState hook and how you benefit from using it.

    Very basic thing such as storing one value in the state in class component is implemented as shown in the picture below:

    In class component state is an object which can have multiple fields and is initialized in the constructor method. According to ES6 you must first invoke super before using this, so this part is also mandatory. Binding methods in the constructor is also needed.

    By converting class to function component and using useState hook, implementation of the exactly same thing looks like this:

    useState declares state variable year, so there is no need to access state object, nor to use constructor method. Simple change of year variable by calling function setYear() changes its value and triggers re-rendering of the component.

    Using useState hook is easier to work with and make the code much cleaner and more readable.

    There is much more to it

    Seeing this basic example is probably not enough to realize how powerful hooks are.

    They are used for solving much bigger problems, such as following:
    – Decomposing huge components that are hard to refactor and test by creating reusable code
    – Removing duplicated logic that is usually result of implementing lifecycle methods
    – Solving “wrapper hell” which is created with using complex patterns like render props and higher-order components

    There is a lot more about hooks that needs to be covered. True power of hooks you can see in some of next blog posts where it will be explained how to replace component’s lifecycle methods with useEffect hook.

    Author: Selena Matijević, Software Engineer at enjoy.ing

    Previous Next

    Related Posts

    Swiss banking: Innovation shaping the future

    Swiss banking: Innovation shaping the future

    We love running

    We love running

    Hands-on blockchain: Ethereum Swarm & Tezos

    Hands-on blockchain: Ethereum Swarm & Tezos

    New enjoy.ing office in Niš

    New enjoy.ing office in Niš

    One of the ways to catch a wide range of the Radio Signals….

    One of the ways to catch a wide range of the Radio Signals….

    The enthusiasts from enjoy.ing created a mobile application for the Belgrade Marathon

    The enthusiasts from enjoy.ing created a mobile application for the Belgrade Marathon

    enjoy.ing at „Voxxed Days“

    enjoy.ing at „Voxxed Days“

    The best way to start your career or at least to start thinking about it.

    The best way to start your career or at least to start thinking about it.

    Dynamics of working in an international team

    Dynamics of working in an international team

    enjoy.ing Family day 2018

    enjoy.ing Family day 2018

    NSM Engineering AG strengthens its executive board with Oliver Brack and Arnoud A. Douw

    NSM Engineering AG strengthens its executive board with Oliver Brack and Arnoud A. Douw

    Sit up and take notes – It’s online onboarding time!

    Sit up and take notes – It’s online onboarding time!

    enjoy.ing wants to interview you: Please, send us your Skype nick

    enjoy.ing wants to interview you: Please, send us your Skype nick

    Heapcon 2019

    Heapcon 2019

    Open Day Belgrade 2018

    Open Day Belgrade 2018

    enjoy.ing internship – Speak up.

    enjoy.ing internship – Speak up.

    ITkonekt 2016 – Origami boat, exclusive sunglasses & lots of fun

    ITkonekt 2016 – Origami boat, exclusive sunglasses & lots of fun

    ElasticSearch Main Objectives

    ElasticSearch Main Objectives

    Wooden labyrinth as a challenge for engineers

    Wooden labyrinth as a challenge for engineers

    Enjoy the Ride Party 2020

    Enjoy the Ride Party 2020

    Open Day Niš 2018

    Open Day Niš 2018

    Enjoy Open.ing

    Enjoy Open.ing

    Fun methods to work on your arithmetic skills

    Fun methods to work on your arithmetic skills

    Family day 2019

    Family day 2019

    Logic behind Chatbot

    Logic behind Chatbot

    Enjoy the Ride company event

    Enjoy the Ride company event

    celebrat.ing 5 years

    celebrat.ing 5 years

    The remarkable women who made a difference

    The remarkable women who made a difference

    The remarkable women who made a difference

    The remarkable women who made a difference

    Job Fair in Nis 2020

    Job Fair in Nis 2020

    Memory management and data persistence mechanism in Exlibris ecommerce platform and bookstore

    Memory management and data persistence mechanism in Exlibris ecommerce platform and bookstore

    Job Fair 2015

    Job Fair 2015

    For the second year running, enjoy.ing supports the Belgrade Marathon

    For the second year running, enjoy.ing supports the Belgrade Marathon

    Open Day Kragujevac 2019

    Open Day Kragujevac 2019

    Open Day 2015

    Open Day 2015

    Meet.ing ElasticSearch – the most popular SE in the world

    Meet.ing ElasticSearch – the most popular SE in the world

    Belgrade Marathon 2019

    Belgrade Marathon 2019

    Enjoying the new normal enjoy.ing

    Enjoying the new normal enjoy.ing

    enjoy.ing and Stanton Chase hosted HR professionals

    enjoy.ing and Stanton Chase hosted HR professionals

    Officially opened new premises in GTC 41

    Officially opened new premises in GTC 41

    Job Fair Nis: Record-breaking number of students visited enjoy.ing booth!

    Job Fair Nis: Record-breaking number of students visited enjoy.ing booth!

    We enjoyed connecting at IT konekt 2017

    We enjoyed connecting at IT konekt 2017

    Job Fair Niš 2019

    Job Fair Niš 2019

    Decade and a half of Ivan Kun

    Decade and a half of Ivan Kun

    VDB16 – Silver sponsor with gold coins

    VDB16 – Silver sponsor with gold coins

    What is shaping the future of education?

    What is shaping the future of education?

    enjoy.ing party 2019

    enjoy.ing party 2019

    The importance of Page object model in test scripts

    The importance of Page object model in test scripts

    Fantastic CODEstantine 2019

    Fantastic CODEstantine 2019

    Open day 2016 with our new bend “Null Pointer and The Exceptions”

    Open day 2016 with our new bend “Null Pointer and The Exceptions”

    Java 8 Workshop

    Java 8 Workshop

    How to deploy a React app without a server, easy, no compromises

    How to deploy a React app without a server, easy, no compromises

    We enjoy connecting

    We enjoy connecting

    Follow us
    enjoy.ing

    Copyright © 2021 enjoy.ing