I set state(id) on button click and i want to use that data(id) in that state, it just showing undefined in the first click
[State Initialization]
const [id,setId] = useState("");
[Onclick of Button]
`render: (record) => (
<div>
<Button
className="details-btn-approve"
onClick={()=>{
handleApprove(record)
}}
>
Approve
</Button>
)`
[useEffect and calling on click action]
`
useEffect(()=>{
},[id]);
const handleApprove = async (record) => {
setId(record.ID);
console.log(id);
}
`
i was expecting the id to be changing every time i click button
here is the complete Code `
import {useEffect, useState} from "react";
import {Button, Card, Input, Modal, Table} from "antd";
import axios from "axios";
import {endPoints} from "./ApiEndPoints";
function Approval() {
const [isLoading, setIsLoading] = useState(false);
const [dataSource, setDataSource] = useState([]);
const [searchedText,setSearchedText] = useState("");
const [id,setId] = useState("");
useEffect(()=>{
ViewUnApproved();
},[setDataSource])
const ViewUnApproved = async () =>{
setIsLoading(true);
const {data: response} = await axios.get(
endPoints.pendingApprovalAsset
);
if (response["status"] === 100){
setIsLoading(false)
setDataSource(response["pending_approval"]);
} else if(response["status"] === 104){
console.log('no data ')
}
}
useEffect(()=>{
},[id])
const handleApprove = async (record) => {
setId(record.ID);
const data = {
ASSET_ID: id,
}
const {data: response } = await axios.post(
endPoints.approve,
data
);
if((response["status"] = 100)){
setIsLoading(false);
console.log(response.to_be_approved);
}
}
console.log(id);
const columns = [
{
title: "Asset",
dataIndex: "DESCRIPTION",
key: "name",
filteredValue:[searchedText],
onFilter:(value,record)=>{
return String(record.DESCRIPTION)
.toLowerCase()
.includes(value.toLowerCase()) ||
String(record.TAG_NUMBER)
.toLowerCase()
.includes(value.toLowerCase()) ||
String(record.BUILDING_NAME)
.toLowerCase()
.includes(value.toLowerCase()) ||
String(record.STATUS)
.toLowerCase()
.includes(value.toLowerCase()) ||
String(record.CONDITION)
.toLowerCase()
.includes(value.toLowerCase())
}
},
{
title: "Tag",
dataIndex: "TAG_NUMBER",
key: "tag",
},
{
title: "Location",
dataIndex: "BUILDING_NAME",
key: "location",
},
{
title: "Status",
dataIndex: "STATUS",
key: "status",
},
{
title: "Condition",
dataIndex: "CONDITION",
key: "condition",
},
{
title: "Action",
key: "",
render: (record) => (
<div>
<Button
className="details-btn-approve"
onClick={()=>{
handleApprove(record)
}}
>
Approve
</Button>
<Button
className="details-btn-reject"
id={record.ID}
type="danger"
onClick={() => {
//showModal(record);
}}
>
Reject
</Button>
</div>
),
},
];
return (
<>
<Card
title="LIST OF ASSETS TO BE APPROVED"
bordered={false}
className="table-card"
>
<Input.Search className='search-input'
placeholder ="Search here ..."
onSearch={(value)=>{
setSearchedText(value)
}}
onChange = {(e)=>{
setSearchedText(e.target.value)
}}
/>
<Table
loading={isLoading}
columns={columns}
dataSource={dataSource}
pagination={{ pageSize: 10 }}
/>
</Card>
</>
);
}
export default Approval;
`