I don't know exactly what is my problem.
I used some library.
When i use callback
and trying to get state of Component
, the state always is default value.
Example when i use mui-datatables
https://github.com/gregnb/mui-datatables
import React, { useState } from 'react'
import MUIDataTable from "mui-datatables";
const columns = [
{
name: "name",
label: "Name",
},
{
name: "company",
label: "Company",
},
{
name: "city",
label: "City",
},
{
name: "state",
label: "State",
},
];
const data = [
{ name: "Joe James", company: "Test Corp", city: "Yonkers", state: "NY" },
{ name: "John Walsh", company: "Test Corp", city: "Hartford", state: "CT" },
{ name: "Bob Herm", company: "Test Corp", city: "Tampa", state: "FL" },
{ name: "James Houston", company: "Test Corp", city: "Dallas", state: "TX" },
];
const MyComponent = () => {
const [myState, setMyState] = useState("default value");
const onRowClick = (rowData, rowMeta) => {
console.log(myState) //always default value
}
const options = {
onRowClick: onRowClick
}
return (
<div>
<MUIDataTable
title={"Employee List"}
data={data}
columns={columns}
options={options}
/>
<button onClick={ () => setMyState("State changed")}>Change state</button>
<p>{myState}</p>
</div>
)
}
First, I click button
to change MyState
, the state changed in p
tag.
Next, I click any row to console
MyState
. But, it's always is default value
.
Why?
Code in CodeSanBox: https://codesandbox.io/s/dreamy-germain-tuylh