I am creating a component in React using tsx that uses an interface as its props. Like shown below:
export interface ISummaryStats {
firstName: string,
lastName: string,
}
export class TestComponent extends React.Component<ISummaryStats> {
refreshData = async () => {
const data = await this.getData();
this.props.firstName = data;
};
getData = async () => {
//Call the API to get the data
//! API call goes here
return 'Jimmy';
}
public render(): React.ReactNode {
return (
<div>
<h1>
{this.props.firstName}
</h1>
<h2>
{this.props.lastName}
</h2>
<button onClick={this.refreshData}>Click me</button>
</div>
);
}
}
What I am trying to do is create a button that calls my API to see if there has been any changes to the data. If so, I want to update the display. However, with the current code I get the following error: Cannot assign to 'firstName' because it is a read-only property.
I am not that experience with interfaces, should I be storing the props values in an object when the component loads so that I can modify that instead of trying to modify the props or is there a different method?