问题描述
npm run dev:h5 编译成功,但是页面报错 Cannot read property 'dispatch' of undefined at Connect.initSelector
复现步骤
npm run dev:h5
但是项目中npm run dev:wepp 编译正常,小程序运行也正常
期望行为
期望,方便的花给出解决方案,谢谢
报错信息
Uncaught TypeError: Cannot read property 'dispatch' of undefined
at Connect.initSelector (index.esm.js?9e15:572)
at new Connect (index.esm.js?9e15:500)
at mountComponent (index.esm.js?5b59:1708)
at ComponentWrapper.init (index.esm.js?5b59:2137)
at createElement (index.esm.js?5b59:1398)
at mountVNode (index.esm.js?5b59:1699)
at Object.render (index.esm.js?5b59:2051)
at eval (app.js?d0a8:347)
at Module../.temp/app.js (app.7629dc6629fef4869bdf.js:927)
at __webpack_require__ (app.7629dc6629fef4869bdf.js:767)
系统信息
👽 Taro v1.3.12
Taro CLI 1.3.12 environment info:
System:
OS: macOS 10.14.5
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 12.4.0 - ~/.nvm/versions/node/v12.4.0/bin/node
Yarn: 1.17.3 - ~/.nvm/versions/node/v12.4.0/bin/yarn
npm: 6.9.0 - ~/.nvm/versions/node/v12.4.0/bin/npm
npmPackages:
@tarojs/async-await: 1.3.12 => 1.3.12
@tarojs/components: 1.3.12 => 1.3.12
@tarojs/plugin-babel: 1.3.12 => 1.3.12
@tarojs/plugin-csso: 1.3.12 => 1.3.12
@tarojs/plugin-sass: 1.3.12 => 1.3.12
@tarojs/plugin-uglifyjs: 1.3.12 => 1.3.12
@tarojs/redux: 1.3.12 => 1.3.12
@tarojs/redux-h5: 1.3.12 => 1.3.12
@tarojs/router: 1.3.12 => 1.3.12
@tarojs/taro: 1.3.12 => 1.3.12
@tarojs/taro-alipay: 1.3.12 => 1.3.12
@tarojs/taro-h5: 1.3.12 => 1.3.12
@tarojs/taro-swan: 1.3.12 => 1.3.12
@tarojs/taro-tt: 1.3.12 => 1.3.12
@tarojs/taro-weapp: 1.3.12 => 1.3.12
@tarojs/webpack-runner: 1.3.12 => 1.3.12
eslint-config-taro: 1.3.12 => 1.3.12
eslint-plugin-taro: 1.3.12 => 1.3.12
nerv-devtools: ^1.4.0 => 1.4.3
nervjs: ^1.4.0 => 1.4.3
stylelint-config-taro-rn: 1.3.12 => 1.3.12
stylelint-taro-rn: 1.3.12 => 1.3.12
欢迎提交 Issue~
如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏
如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。
Good luck and happy coding~
CC @Littly
没有复现,麻烦提供代码。
哪一块儿的代码?
报错信息点开是这个文件webpack:///node_modules/_@[email protected]@@tarojs/redux-h5/dist/index.esm.js?9e15
Hello~
您的问题所提供的信息不足,我们无法定位到具体的问题。如果有空的话还请拔冗提供更具体的信息,否则这个 issue 将在 15 天后被自动关闭。
如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。
Good luck and happy coding~
哪一块儿的代码?
页面的代码,或者可以复现问题的最小仓库
检查下是不是组件的构造函数把context传丢了吧
我也遇到这问题了。 1.3.20 伤心
同遇到了这个问题, 我的问题是在 app.js 上面直接使用了@connect(mapState, mapDispatch).
解决方案是不写这两行, 因为app.js 上会注入store 实例。 需要dispatch 或者拿数据的话,直接调用store对应的方法
我可以正常实现,在我修改app.jsx之前,我也遇到来题主的问题。
我说一下我的实现结果,我使用的是Taro v2.2.6 (我认为在v2.0.0-2.2.6都可以正常运行):
//app.jsx
import { Provider } from '@tarojs/redux'
import configStore from './store'
const store = configStore()
class App extends Component {
//....
render () {
return (
<Provider store={store}>
<Index />
</Provider>
)
}
}
具体的业务pages /home.jsx
import Taro, { Component } from "@tarojs/taro";
import * as actions from '@actions/home'
import { connect } from "@tarojs/redux";
@connect(state => state.home, { ...actions })
class Home extends Component {
}
检查下是不是组件的构造函数把context传丢了吧
我也遇到了,debugger了下源码,定位到是父组件的context丢失
造成原因:应该是父组件这样写了
constructor(props){
super(props)
}
导致只传递的props,context丢失
按下面这样就可以了
constructor(){
super(...arguments)
}
Most helpful comment
同遇到了这个问题, 我的问题是在 app.js 上面直接使用了@connect(mapState, mapDispatch).
解决方案是不写这两行, 因为app.js 上会注入store 实例。 需要dispatch 或者拿数据的话,直接调用store对应的方法