gpt4 book ai didi

javascript - 将 Redux 连接到我的 React APP : Actions may not have an undefined "type" property

转载 作者:行者123 更新时间:2023-12-03 01:16:56 30 4
gpt4 key购买 nike

我是 Redux 新手,在尝试将 Redux 连接到我的 React 应用程序时遇到很多问题,每当我在输入框中输入内容时,我都会收到此错误:

Uncaught Error: Actions may not have an undefined "type" property. Have you misspelled a constant? at dispatch (eval at ./node_modules/redux/es/createStore.js (bundle.js:4248), :164:13) at handleSearchTermChange (eval at ./js/Landing.jsx (bundle.js:757), :78:7) at Object.executeOnChange (eval at ./node_modules/react-dom/lib/LinkedValueUtils.js (bundle.js:2779), :132:34) at ReactDOMComponent._handleChange (eval at ./node_modules/react-dom/lib/ReactDOMInput.js (bundle.js:2899), :239:38) at Object.ReactErrorUtils.invokeGuardedCallback (eval at ./node_modules/react-dom/lib/ReactErrorUtils.js (bundle.js:3019), :69:16) at executeDispatch (eval at ./node_modules/react-dom/lib/EventPluginUtils.js (bundle.js:2739), :85:21) at Object.executeDispatchesInOrder (eval at ./node_modules/react-dom/lib/EventPluginUtils.js (bundle.js:2739), :108:5) at executeDispatchesAndRelease (eval at ./node_modules/react-dom/lib/EventPluginHub.js (bundle.js:2723), :45:22) at executeDispatchesAndReleaseTopLevel (eval at ./node_modules/react-dom/lib/EventPluginHub.js (bundle.js:2723), :56:10) at Array.forEach ()

这是我的 Landing.jsx:

// @flow

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import type { RouterHistory } from 'react-router-dom';
import { setSearchTerm } from './actionCreators';

class Landing extends Component {
props: {
searchTerm: string,
handleSearchTermChange: Function,
history: RouterHistory
};
goToSearch = (event: SyntheticEvent) => {
event.preventDefault();
this.props.history.push('/search');
};
render() {
return (
<div className="landing">
<h1>svideo</h1>
<form onSubmit={this.goToSearch}>
<input
onChange={this.props.handleSearchTermChange}
value={this.props.searchTerm}
type="text"
placeholder="Search"
/>
</form>
<Link to="/search">or Browse All</Link>
</div>
);
}
}

const mapStateToProps = state => ({ searchTerm: state.searchTerm });
const mapDispatchToProps = (dispatch: Function) => ({
handleSearchTermChange(event) {
dispatch(setSearchTerm(event.target.value));
}
});

export default connect(mapStateToProps, mapDispatchToProps)(Landing);

这是我的 store.js

import { createStore } from 'redux';
import reducers from './reducers';

const store = createStore(reducers);

export default store;

这是我的action.js 文件:

/* This file will be a bunch of constants*/
export const SET_SEARCH_TERM = 'SET_SEARCH_TERM';

我的actionCreators.js

import { SEARCH_SEARCH_TERM } from './actions';

/* Takes in a "searchterm" and it returns type and payload */

export function setSearchTerm(searchTerm: string) {
return { type: SEARCH_SEARCH_TERM, payload: searchTerm };
}

还有reducer.js:

import { SET_SEARCH_TERM } from './actions';

const DEFAULT_STATE = {
searchTerm: ''
};

const setSearchTerm = (state, action) => Object.assign({}, state, { searchTerm: action.payload });

const rootReducer = (state = DEFAULT_STATE, action: Action) => {
switch (action.type) {
case SET_SEARCH_TERM:
return setSearchTerm(state, action);
default:
return state;
}
};
export default rootReducer;

真的希望有人能在这里启发我,因为我对整个 React、Redux、Flow 和 Type 世界都很陌生。

最佳答案

好吧,问题是基本上,我试图导入一个由于拼写错误而不存在的函数:

这是我的 action.js 文件中的声明:

/* This file will be a bunch of constants*/
export const SET_SEARCH_TERM = 'SET_SEARCH_TERM';

我试图像这样导入 const:

import { SEARCH_SEARCH_TERM } from './actions';

export function setSearchTerm(searchTerm: string) {
return { type: SEARCH_SEARCH_TERM, payload: searchTerm };
}

SEARCH_SEARCH_TERM 未定义,我将其更改为 SET_SEARCH_TERM,现在它可以正常工作。感谢@markerikson 对原始帖子的评论。

关于javascript - 将 Redux 连接到我的 React APP : Actions may not have an undefined "type" property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970053/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com