Giter Site home page Giter Site logo

cyclecraft's Introduction

React Hooks

useState -

useState is React Hook that allows you to add state to a functional component. It returns an array with two values: the current state and a function to update it. The Hook takes an initial state value as an argument and returns an updated state value whenever the setter function is called.

In Strict Mode, React will call your initializer function twice in order to help you find accidental impurities. This is development-only behavior and does not affect production. If your initializer function is pure (as it should be), this should not affect the behavior. The result from one of the calls will be ignored.

const ExploreGarage = () =>{
    return (
        <div className = 'explore-garge' id= 'explore-garge'>
        <h1>Explore Our Garage<h1/>
        <p className='explore-garage-text'>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Provident distinctio earum totam eius! Saepe sit veniam porro, obcaecati ipsa molestias voluptates et ea illum doloribus.</p>
        <div className = 'explore-garge-lise'>
        {Garage_list.map(item, index) =>{
            return(
                <div key = {index} className = 'explore-garage-list-item'>
                <img src ={item.bike_image} alt= ''/>
                <p>{item.bike_name}<p>
            )
        }}
        </div>

    )
}

ContextAPI

lets say you have created four componentes; Component A, Component B, Component C, Component D

Now in component A and there you have created a state which has a function> which has value, Key. Now you want to access that state in component D. The traditional method to resolve this problem is you will give that state to Component B > then > Component C > then > Component D.

But this create a problem in React which we call {propsDrilling}. We are unecessary passing the state to B & C component. That makes code look ugly. To fix it we need a method where we can dirctly use that method in Component D withour passing it down to B & C. That's when contextAPI comes in picture.

Now what we can do is create that State in sperate Context API file. Then we can use that state variable in Component A & D without drilling it down. Just passing it to both components using context API. Also if the state has been change in Components A the state will automatically will be changed in Component D. Becaue the Component D is a child of Component A. Once the Component A re-renders it will push component D to re-render itself.

To use this feature you have to make sure that you wrap all the component (A,B,C,D) inside the tags of {ContxtProvider}

const [category, useCategory] = useState("All");
<div onClick= {prev => setCategory(prev.value === ?)}></div>

cyclecraft's People

Contributors

ashish-k-gupta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.