如何在 ReactJS 中使用 mobx 组织打字稿

How to organize typescript with mobx at ReactJS

我做了如下 mobx 存储。
我在 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;
  };
}

然后我将这个商店注入到一个组件中。
我在这里声明了 UserInformation 类型。
但是'const { SignStore } = this.props;'这里,
SignStore 说

Property 'SignStore' does not exist on type 'Readonly & Readonly<{ children?: ReactNode; }>'.

import * as React from 'react';
import { observer, inject } from 'mobx-react';

interface IUserInformation {
  email: string;
  password: string;
}

@inject('SignStore')
@observer
class SignUp extends React.Component<IUserInformation> {
  render() {
    const { SignStore } = this.props;

    return (
      <div className="container">
        <form className="white" onSubmit={}>
          <h5 className="grey-text text-darken-3">Sign Up</h5>
          <div className="input-field">
            <label htmlFor="email">Email</label>
            <input type="email" id="email" onChange={SignStore.handleChange} />
          </div>
          <div className="input-field">
            <label htmlFor="password">Password</label>
            <input
              type="password"
              id="password"
              onChange={SignStore.handleChange}
            />
          </div>

        </form>
      </div>
    );
  }
}

export default SignUp;

在这种情况下你能推荐一些建议吗?

除其他问题外,主要问题是 this.props 属于 IUserInformation 类型,即 { email: string; password: string; } 然后您尝试用 const { SignStore } = this.props; 显然 [=17] 解构它=] 没有 SignStore 参数。为此道具需要看起来像:{ email: string; password: string; SignStore: any }.

我不太熟悉 mobx,但看起来你至少需要一个 ISignStore 接口:

interface ISignStore {
  UserInformation: IUserInformation;
  handleChange: (e: any) => void;
}

然后将其用于您的组件:

class SignUp extends React.Component<ISignStore>

并像这样使用它:

const SignStore = this.props;