如何在ReactJS上使用mobx组织typescript
我制作了如下的mobx商店。如何在ReactJS上使用mobx组织typescript,reactjs,typescript,mobx,Reactjs,Typescript,Mobx,我制作了如下的mobx商店。 我在store.js中声明了操作类型 import { action, observable } from 'mobx'; export class SignStore { @observable UserInformation: { email: ''; password: ''; }; @action handleChange = (e: any): void => { this.UserInformatio
我在store.js中声明了操作类型
import { action, observable } from 'mobx';
export class SignStore {
@observable
UserInformation: {
email: '';
password: '';
};
@action
handleChange = (e: any): void => {
this.UserInformation[e.target.id] = e.target.value;
};
}
我将这个存储注入一个组件中。我在这里声明了用户信息的类型。
但是'const{SignStore}=this.props;'这里,
招牌店说 类型“Readonly&Readonly”上不存在属性“SignStore”
import*as React from'React';
从“mobx react”导入{observer,inject};
接口信息{
电子邮件:字符串;
密码:字符串;
}
@注入('SignStore')
@观察者
类注册扩展了React.Component{
render(){
const{SignStore}=this.props;
返回(
注册
电子邮件
密码
);
}
}
导出默认注册;
在这种情况下,你能推荐一些建议吗?除其他问题外,主要问题是
this.props
是IUserInformation
类型,即{email:string;password:string;}
然后尝试用const{SignStore}=this.props代码>显然props
没有SignStore
参数。为此,道具需要看起来像:{email:string;password:string;SignStore:any}
我对mobx不是很熟悉,但似乎您至少需要一个ISignStore的界面:
interface ISignStore {
UserInformation: IUserInformation;
handleChange: (e: any) => void;
}
然后将其用于您的组件:
class SignUp extends React.Component<ISignStore>
非常感谢。它工作得很好。那是我必须再次申报的!
class SignUp extends React.Component<ISignStore>
const SignStore = this.props;