0

我目前正在使用类型化的 React (TSX) 和 mobx 进行状态管理。

我能够构建一个同时使用观察者和注入装饰器的组件。但是我无法构建一个在没有观察者的情况下使用注入的组件。

这通过了打字稿编译器

export const DealershipBreadCrumb = inject("appStore")(observer((props: Props) => {
    const {appStore} = props;
    const dealership = appStore.getSelectedDealership();
    return (
          <div className="filter__container filter__group">
            <a className="filter__link" href={`cars?q=${appStore.searchResults.searchQuery}`}>
              <span className="filter__text">{dealership.name}</span>
            </a>
          </div>
      )
    }))

然而这失败了

export const DealershipBreadCrumb = inject("appStore")((props: Props) => {

带有以下错误消息

[ts] Argument of type '(props: Props) => Element' is not assignable to parameter of type 'ComponentClass<{}>'.
   Type '(props: Props) => Element' provides no match for the signature 'new (props?: {}, context?: any): Component<{}, ComponentState>'

请协助我制作此错误消息的正面和反面。我敢打赌,打字已经过时了。否则在没有观察者的情况下使用注入实际上是无效的组合。

4

1 回答 1

0

我认为您的错误来自此:((props: Props)... 当以这种方式使用注入时,我相信它需要一个函数,请尝试像这样使用它:
export const DealershipBreadCrumb = inject('SystemDataStore')((props => {

顺便说一句,不要忘记,如果您想在没有观察者的情况下使用注入,它将注入该存储的最后一个值,但它不会知道值的任何变化。

于 2016-11-10T09:25:59.280 回答