0

问题:尝试将 MST 存储注入反应类组件时出现错误:

Error: Uncaught [Error: MobX injector: Store 'auth' is not available! Make sure it is provided by some Provider

错误源于下面发布的 LoginForm 组件,并嵌套在另一个组件下(不确定这是否是问题,也就是说每个组件都需要一个<Provider>?)

组件层次结构:

Application (contains no Provider)
    |__ Authentication (contains no Provider)
        |__LoginForm

一点代码:

import { AuthenticationStore } from './stores/AuthenticationStore'

const auth = AuthenticationStore.create()

ReactDOM.render(
  <Provider auth={auth}>
    <Application />
  </Provider>,
  document.getElementById('root')
);


//Application
const Application = inject('auth')(observer(class Application extends 
Component {
  render() {
    console.log(this.props.auth) // logs the store as expected
    return (
      <div className={styles.applicationContainer}>
        <Authentication/>
      </div>
    )
  }
}))

export default Application

// LoginForm
const LoginForm = inject('auth')(observer(class LoginForm extends Component {
  render() {
    return (
       <div></div>
    )
  }
}))

export default LoginForm

和问题:

如果不是这样,将商店传递给子组件(不作为道具传递)的首选协议是什么?

与往常一样,提前感谢您的任何和所有输入。如果这是重复的(我找不到这个特定的错误),请标记为这样,我会相应地更新......

4

0 回答 0