DEV Community

Manoj Manoharan
Manoj Manoharan

Posted on

Use push method without push ! in react

import { Fragment, useState } from "react";

const Login = () =>{

    const [data,setData] = useState([]); // step 1 create useState
    // An obj to push
    const obj = { 
        name:"manojconcept",
        age:"unlimited"
    }
    // Adding HandleClick function
    const handClick = () =>{

        setData([...data,obj]) // by using spread operator

    }

    return(
        <>
        <button onClick={handClick}>
            click Me
        </button>
        {
            data.map((ele,ind)=>{
                return(
                    <Fragment key={ind}>
                        <p>
                            {ele.name}
                        </p>
                        <p>
                            {ele.age}
                        </p>

                    </Fragment>

                )
            })
        }
        </>
    )
}

export default Login;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)