I want to get the result of a query and use it for a button function, to change the state of the parent component. In this code example, the state of the parent component contains an ID which is used to display some stuff later. I want to create a button for every set of data of my data base and use the ID of the sets to change the state. But when I implement it like this the ID changes to "NaN". Is there any possible way to make this work?
class TestClass extends React.Component{
constructor(props) {
super(props);
this.handleNewID = this.handleNewID .bind(this);
this.state = {
ID: 0,
};
}
handleNewID ({id}){
this.setState({ID: id});
}
render(){
const props={
handleNewID :this.handleNewID ,
}
return(
<div>
<TestFunction props={props}/>
</div>)
}
}
function TestFunction ({props}){
const {loading, error, data} = useQuery(GET_SOMETHING);
if (loading) return <p>Loading ...</p>;
if (error) return <p>Error ... ${JSON.stringify(error, null, 2)}</p>;
if (data)
return data.something.map(({ id }) => (
<div>
<button onClick={ () => props.handleNewID ({id})}> Test Button </button>
</div>));
}