dvajs / dva

🌱 React and redux based, lightweight and elm-style framework. (Inspired by elm and choo)
https://dvajs.com/
MIT License
16.24k stars 3.17k forks source link

Invariant Violation: Could not find "store" in either the context or props #2420

Closed jacktang closed 3 years ago

jacktang commented 3 years ago

环境

Taro 3.0.21 + dva 2.4.1

代码

import "taro-ui/dist/style/components/search-bar.scss"; import "taro-ui/dist/style/components/list.scss"; import "taro-ui/dist/style/components/icon.scss";

function Index( props ) { const { dispatch } = props; useEffect(() => { initData(); }, []);

const initData = (cb) => { dispatch({type: 'companies/search'}).then(() => { cb && cb() }) };

const search = (text) => { };

return ( <> <AtSearchBar placeholder='输入公司名进行搜索' onChange={search} />

</>

) };

function mapStateToProps(state) { return { ...state }; } const IndexPage = connect(mapStateToProps)(Index); export default IndexPage;


在app.js已经提供store
```js
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import Taro from '@tarojs/taro';
import dva from './dva';
import models from './models';
import * as user from './utils/user';

import configStore from './store'

import './app.scss'

// const store = configStore()
const dvaApp = dva.createApp({
  initialState: {},
  models: models,
  onError(e, dispatch) {
    console.log('系统出错了!');
    // dispatch(action("sys/error", e));
  },
});
const store = dvaApp.getStore();

class App extends Component {
  componentDidMount () {}

  componentDidShow () {
    user.checkLogin().then(res => {
    }).catch(() => {
      console.log('not login');
      Taro.navigateTo({
        url: '/pages/login/index'
      });
    });
  }

  componentDidHide () {}

  componentDidCatchError () {}

  render () {
    return (
      <Provider store={store}>
        {this.props.children}
      </Provider>
    )
  }
}

export default App

错误

react.ts:53 Invariant Violation: Could not find "store" in either the context or props of "Connect(Index)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Index)".
    at invariant (http://127.0.0.1:19495/appservice/vendors.js:935:15)
    at new Connect (http://127.0.0.1:19495/appservice/pages/companies/index.js:5559:57)
    at Zc (http://127.0.0.1:19495/appservice/vendors.js:2164:171)
    at xe (http://127.0.0.1:19495/appservice/vendors.js:2203:149)
    at ig (http://127.0.0.1:19495/appservice/vendors.js:2281:396)
    at hg (http://127.0.0.1:19495/appservice/vendors.js:2258:22)
    at bg (http://127.0.0.1:19495/appservice/vendors.js:2257:366)
    at Of (http://127.0.0.1:19495/appservice/vendors.js:2251:214)
    at http://127.0.0.1:19495/appservice/vendors.js:2149:115
    at push../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (http://127.0.0.1:19495/appservice/vendors.js:3164:438)
jacktang commented 3 years ago

使用 class component和 @connect 是可以的

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.