我编写了一个小型便利类,以集中我们对应用程序进行的所有电子邮件验证。
但是,当我运行它时,出现控制台错误:
无法读取未定义的属性“存储”
我在这里显然缺少导入,但是不清楚如何导入store
。我的直觉告诉我,我在这里做错了。在过去的四年中使用移动设备后重新回到网络,所以我有点生疏:)
谢谢
import React from 'react';
import { connect } from 'react-redux';
import { checkEmail } from 'app/auth/redux/actions.js';
class EmailValidator extends React.Component {
constructor(props) {
super(props);
// via: https://stackoverflow.com/a/13178771/602210
this.regex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
}
// Tests whether the email is in a valid format like name@example.com
emailIsValid(email) {
return this.regex.test(email);
}
* loginEmailExists(email) {
const { emailIsTaken } = yield this.props.checkEmail(email);
return emailIsTaken;
}
}
const mapDispatchToProps = dispatch => {
return {
checkEmail: (email) => dispatch(checkEmail(email)),
};
};
export default connect(null, mapDispatchToProps)(EmailValidator);
然后在另一个组件中实现:
import EmailValidator from 'app/helpers/EmailValidator';
在组件的下部,在一个函数中:
const validator = new EmailValidator();
if (!validator.emailIsValid(email)) { ... } // throws error below
完全错误
connectAdvanced.js:123 Uncaught TypeError: Cannot read property 'store' of undefined
at new Connect (connectAdvanced.js:123)
at eval (index.js:149)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:270)
at executeDispatch (react-dom.development.js:561)
at executeDispatchesInOrder (react-dom.development.js:583)
at executeDispatchesAndRelease (react-dom.development.js:680)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:688)
Connect(EmailValidator) @ connectAdvanced.js:123
(anonymous) @ index.js:149
callCallback @ react-dom.development.js:149
invokeGuardedCallbackDev @ react-dom.development.js:199
invokeGuardedCallback @ react-dom.development.js:256
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:270
executeDispatch @ react-dom.development.js:561
executeDispatchesInOrder @ react-dom.development.js:583
executeDispatchesAndRelease @ react-dom.development.js:680
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:688
forEachAccumulated @ react-dom.development.js:662
runEventsInBatch @ react-dom.development.js:816
runExtractedEventsInBatch @ react-dom.development.js:824
handleTopLevel @ react-dom.development.js:4826
batchedUpdates$1 @ react-dom.development.js:20233
batchedUpdates @ react-dom.development.js:2151
dispatchEvent @ react-dom.development.js:4905
(anonymous) @ react-dom.development.js:20284
unstable_runWithPriority @ scheduler.development.js:255
interactiveUpdates$1 @ react-dom.development.js:20283
interactiveUpdates @ react-dom.development.js:2170
dispatchInteractiveEvent @ react-dom.development.js:4882
react-dom.development.js:289 Uncaught TypeError: Cannot read property 'store' of undefined
at new Connect (connectAdvanced.js:123)
at eval (index.js:149)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:270)
at executeDispatch (react-dom.development.js:561)
at executeDispatchesInOrder (react-dom.development.js:583)
at executeDispatchesAndRelease (react-dom.development.js:680)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:688)
答案 0 :(得分:0)
不确定是否正确设置了商店,甚至根本没有设置商店。但是,这是一个示例,说明如何做:
// src/store.js
import { createStore } from 'redux'
import reducer from './reducers'
const enhancer = window.__REDUX_DEVTOOLS_EXTENSION__ &&window.__REDUX_DEVTOOLS_EXTENSION__() // Redux DevTools, a must
const store = createStore(reducer, enhancer)
export default store
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import store from './store'
import {Provider} from 'react-redux'
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.getElementById('root'));
registerServiceWorker();
请记住要使用React.Component
扩展您的课程,例如:
import React from 'react';
...
class EmailValidator extends React.Component {
...
希望这会让您走上正确的道路。您的调度和连接语法很好,因此至少不是问题。
答案 1 :(得分:0)
store
无法导入-需要提供<Provider/>
首先运行简单的redux项目。
您曾经使用过redux吗?
您不能connect
还原存储到任何类,它是HOC组件增强器。
您的EmailValidator
也可以是HOC。