[英]pass data to api react js
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我是 reactjs 的新手,我有一个可点击的 div,我可以在下一步中更改内容我想通过点击编辑按钮传递数据我更改为 api;我该怎么办
<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>
? ?
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.要联系您的 api,您可以使用 Javascript 中的fetch API
。
And I'm guessing that your currentUser
object is a state object, so you can just get that object in your function.而且我猜测您的currentUser
对象是一个状态对象,因此您可以在函数中获取该对象。
<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)) };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.