Home > Net >  pass data to api react js
pass data to api react js

Time:07-07

I'm new in reactjs and I have a clickable div that I can change the content in the next step I want to pass the data with click on edit button I changed to api ;what should I do

 <div style={{ cursor: 'pointer' }} contentEditable="true" className="p-2 ">
  {currentUser.user?.fullName}
 </div>
   <Button variant="success" disabled={!isLoggedIn} className="btn btn-custom w-100 text-center 
  text-black">
      edit
   </Button>

?

CodePudding user response:

You need to create a function that you call when the button is clicked. To contact your api you can use the fetch API from Javascript.

And I'm guessing that your currentUser object is a state object, so you can just get that object in your function.

CodePudding user response:

<div style={{ cursor: 'pointer' }} contentEditable="true" className="p-2 ">
      {currentUser.user?.fullName}
     </div>
       <Button 
         variant="success" 
         disabled={!isLoggedIn} 
         className="btn btn-custom w-100 text-center text-black"
         onClick={handleUser(currentUser)}
       >
          edit
       </Button>

const handleUser = (data) => {
   console.log(data);
  fetch('API_URL')
    .then(res => res.json())
    .then(data => console.log(data))
};

  • Related