according to the documentation, Mobx, wants to get and call parameters in the form of inject in the sub-component, but there is a undefined
problem?
// Timer.ts
interface IProps {
timerStore?: TimerStore;
}
interface State {}
inject("timerStore")
@observer
export default class Timer extends React.Component<IProps, State> {
constructor(props: IProps) {
super(props);
this.state = {};
this.onClickBtn = this.onClickBtn.bind(this)
}
public onClickBtn():void {
const tStore = this.props.timerStore as TimerStore;
console.log(tStore); // undefined
}
public render() {
const tStore = this.props.timerStore as TimerStore;
console.log(tStore); // undefined
return (
<div onClick={this.onClickBtn}>Seconds passed: {1}</div>
)
}
}
in App.ts
, we can get the data normally, but when the sub-component Timer.ts
injects store through inject
, the corresponding store, will be undefined
. After checking many reasons, we don"t know what the problem is. Ask the building to help solve it?