Uncaught TypeError: Cannot read property 'type' of undefined at start of switch function
Uncaught TypeError: Cannot read property 'type' of undefined at start of switch function
我正在尝试使用将我的应用程序从纯 React 移动到 redux-react。我为 onClick 做了一个动作和缩减器,但是在尝试以开发模式启动应用程序后我得到了这个错误
Uncaught TypeError: Cannot read property 'type' of undefined at reducerDomMethods (manMethodsReducers.js:12)
这是哪一行
switch (action.type) {
这是我的代码
减速机
export default function reducerDomMethods(state={
isClicked: false,
}, action) {
switch (action.type) {
case "CLICK_OPEN": {
return {
...state,
isClicked: true
}
}
case "CLICK_CLOSE": {
return{
...state,
isClicked:false
}
}
return state;
}
}
动作
export function clicking(isClicked) {
return function (dispatch) {
if( isClicked === true){
dispatch({type: "CLICK_OPEN",isClicked: true});
}else {
dispatch({type: "CLICK_CLOSE",isClicked: false});
}
}
}
联合减速机
import { combineReducers } from "redux"
import cityName from "./apiReducers"
import nameOfCity from "./apiReducers"
import weatherDescription from "./apiReducers"
import windSpeed from "./apiReducers"
import temperature from "./apiReducers"
import maxTemperature from "./apiReducers"
import minTemperature from "./apiReducers"
import isClicked from "./manMethodsReducers"
export default combineReducers({
cityName,
nameOfCity,
weatherDescription,
windSpeed,
temperature,
maxTemperature,
minTemperature,
isClicked
})
商店
import { applyMiddleware, createStore } from "redux"
import logger from "redux-logger"
import thunk from "redux-thunk"
import promise from "redux-promise-middleware"
import reducer from "./reducers"
import reducerDomMethods from "./reducers"
const middleware = applyMiddleware(promise(), thunk, logger())
export default createStore( reducer , reducerDomMethods, middleware)
连接
import {connect} from "react-redux"
@connect((store) => {
return {
nameOfCity:store.nameOfCity.nameOfCity,
weatherDescription:store.weatherDescription.weatherDescription,
windSpeed:store.windSpeed.windSpeed,
temperature:store.temperature.temperature,
maxTemperature:store.maxTemperature.maxTemperature,
minTemperature:store.minTemperature.minTemperature,
isClicked:store.isClicked.isClicked,
}
})
编辑:这是我导入发送操作的地方
import React, {Component} from 'react';
import SearchBar from '../components/SearchBar';
import {connect} from "react-redux"
import {fetchWeatherData} from "../actions/weather-apiActions";
import {clicking} from '../actions/manMethodsActions'
@connect((store) => {
return {
nameOfCity:store.nameOfCity.nameOfCity,
weatherDescription:store.weatherDescription.weatherDescription,
windSpeed:store.windSpeed.windSpeed,
temperature:store.temperature.temperature,
maxTemperature:store.maxTemperature.maxTemperature,
minTemperature:store.minTemperature.minTemperature,
isClicked:store.isClicked.isClicked,
}
})
class FormContainer extends Component {
handleFormSubmit(e) {
e.preventDefault();
var cName = e.target.CityName.value;
console.log(cName);
let isClicking = false;
this.props.dispatch(clicking(isClicking));
this.props.dispatch(fetchWeatherData(cName));
}
render() {
return (
<div>
<form onSubmit={this.handleFormSubmit.bind(this)}>
<label>{this.props.label}</label>
<SearchBar
name="CityName"
type="text"
value={this.props.cityName}
placeholder="search"
/>
<button type="submit" className="" value='Submit' placeholder="Search">Search</button>
</form>
</div>
);
}
}
export {FormContainer};
您的 clicking
操作 returns 一个函数,您正在使用 this.props.dispatch(clicking(isClicking));
调度该函数。如果你想保留动作的嵌套结构,那么你应该将调度修改为 this.props.dispatch(clicking(isClicking)());
,它会自动调用从 clicking
动作接收到的函数。
但是,建议的用途是修改您的 clicking
操作...
export function clicking(isClicked) {
dispatch({ type: "CLICK_OPEN", isClicked });
}
请记住,您可以在操作文件中导入您的商店并使用 store.dispatch
来发送操作。您不需要从组件传递 dispatch
函数。
我正在尝试使用将我的应用程序从纯 React 移动到 redux-react。我为 onClick 做了一个动作和缩减器,但是在尝试以开发模式启动应用程序后我得到了这个错误
Uncaught TypeError: Cannot read property 'type' of undefined at reducerDomMethods (manMethodsReducers.js:12)
这是哪一行
switch (action.type) {
这是我的代码
减速机
export default function reducerDomMethods(state={
isClicked: false,
}, action) {
switch (action.type) {
case "CLICK_OPEN": {
return {
...state,
isClicked: true
}
}
case "CLICK_CLOSE": {
return{
...state,
isClicked:false
}
}
return state;
}
}
动作
export function clicking(isClicked) {
return function (dispatch) {
if( isClicked === true){
dispatch({type: "CLICK_OPEN",isClicked: true});
}else {
dispatch({type: "CLICK_CLOSE",isClicked: false});
}
}
}
联合减速机
import { combineReducers } from "redux"
import cityName from "./apiReducers"
import nameOfCity from "./apiReducers"
import weatherDescription from "./apiReducers"
import windSpeed from "./apiReducers"
import temperature from "./apiReducers"
import maxTemperature from "./apiReducers"
import minTemperature from "./apiReducers"
import isClicked from "./manMethodsReducers"
export default combineReducers({
cityName,
nameOfCity,
weatherDescription,
windSpeed,
temperature,
maxTemperature,
minTemperature,
isClicked
})
商店
import { applyMiddleware, createStore } from "redux"
import logger from "redux-logger"
import thunk from "redux-thunk"
import promise from "redux-promise-middleware"
import reducer from "./reducers"
import reducerDomMethods from "./reducers"
const middleware = applyMiddleware(promise(), thunk, logger())
export default createStore( reducer , reducerDomMethods, middleware)
连接
import {connect} from "react-redux"
@connect((store) => {
return {
nameOfCity:store.nameOfCity.nameOfCity,
weatherDescription:store.weatherDescription.weatherDescription,
windSpeed:store.windSpeed.windSpeed,
temperature:store.temperature.temperature,
maxTemperature:store.maxTemperature.maxTemperature,
minTemperature:store.minTemperature.minTemperature,
isClicked:store.isClicked.isClicked,
}
})
编辑:这是我导入发送操作的地方
import React, {Component} from 'react';
import SearchBar from '../components/SearchBar';
import {connect} from "react-redux"
import {fetchWeatherData} from "../actions/weather-apiActions";
import {clicking} from '../actions/manMethodsActions'
@connect((store) => {
return {
nameOfCity:store.nameOfCity.nameOfCity,
weatherDescription:store.weatherDescription.weatherDescription,
windSpeed:store.windSpeed.windSpeed,
temperature:store.temperature.temperature,
maxTemperature:store.maxTemperature.maxTemperature,
minTemperature:store.minTemperature.minTemperature,
isClicked:store.isClicked.isClicked,
}
})
class FormContainer extends Component {
handleFormSubmit(e) {
e.preventDefault();
var cName = e.target.CityName.value;
console.log(cName);
let isClicking = false;
this.props.dispatch(clicking(isClicking));
this.props.dispatch(fetchWeatherData(cName));
}
render() {
return (
<div>
<form onSubmit={this.handleFormSubmit.bind(this)}>
<label>{this.props.label}</label>
<SearchBar
name="CityName"
type="text"
value={this.props.cityName}
placeholder="search"
/>
<button type="submit" className="" value='Submit' placeholder="Search">Search</button>
</form>
</div>
);
}
}
export {FormContainer};
您的 clicking
操作 returns 一个函数,您正在使用 this.props.dispatch(clicking(isClicking));
调度该函数。如果你想保留动作的嵌套结构,那么你应该将调度修改为 this.props.dispatch(clicking(isClicking)());
,它会自动调用从 clicking
动作接收到的函数。
但是,建议的用途是修改您的 clicking
操作...
export function clicking(isClicked) {
dispatch({ type: "CLICK_OPEN", isClicked });
}
请记住,您可以在操作文件中导入您的商店并使用 store.dispatch
来发送操作。您不需要从组件传递 dispatch
函数。