I'm a begginer in React and would like to figure out how to modify values get using props.
f.e:
I have a MobX GameStore.tsx with @observable values:
export class GameStore {
@observable money = 0;
@observable CPS = 0;
@observable taskCodeLines = 0;
@observable taskCodeLinesTarget = 10;
...
@observable staffFrontEndCount = 4;
@observable staffFrontEndStartCost = 100;
@observable staffPHPCount = 2;
@observable staffPHPStartCost = 250;
}
Now I want to have a few StaffMember objects in Staff class:
render() {
return(
<div className="staff">
<ul className="staff-list">
<StaffMember job="Front End Developer" count={ gameStore.staffFrontEndCount } startCost = { gameStore.staffFrontEndStartCost } />
<StaffMember job="PHP Developer" count={ gameStore.staffPHPCount } startCost = { gameStore.staffPHPStartCost } />
</ul>
</div>
);
}
I pass down a data like name of this objects and some values. And now I want to modify some of them, like:
@observer
export default class StaffMember extends React.Component<any, any> {
@computed get increaseStaffCount() {
return this.props.count;
}
@action hireStaff() {
let cost = this.props.startCost * 1.4 * (this.props.count + 1);
if (gameStore.money >= cost) {
gameStore.money -= cost;
this.props.count += 1; // It's illegal because props data is read-only
this.countCPS();
}
}
How can I do this? Is this OK to create logic like above? How should I create instances of classes in react and build a generic methods for them? Thanks for help ;)