Oun*_*wnO 4 javascript ecmascript-6 reactjs redux react-redux
我正在尝试使用将我的应用程序移动到纯反应的redux反应。我为onClick做了一个动作和减速器,但是尝试以开发模式启动应用程序后,出现此错误
Uncaught TypeError: Cannot read property 'type' of undefined at reducerDomMethods (manMethodsReducers.js:12)
Run Code Online (Sandbox Code Playgroud)
这是这条线
switch (action.type) {
Run Code Online (Sandbox Code Playgroud)
这是我的代码
减速器
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;
}
}
Run Code Online (Sandbox Code Playgroud)
行动
export function clicking(isClicked) {
return function (dispatch) {
if( isClicked === true){
dispatch({type: "CLICK_OPEN",isClicked: true});
}else {
dispatch({type: "CLICK_CLOSE",isClicked: false});
}
}
}
Run Code Online (Sandbox Code Playgroud)
联合减速机
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
})
Run Code Online (Sandbox Code Playgroud)
商店
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)
Run Code Online (Sandbox Code Playgroud)
连接
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,
}
})
Run Code Online (Sandbox Code Playgroud)
编辑:这是我导入发送动作的地方
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};
Run Code Online (Sandbox Code Playgroud)
您的clicking操作将返回一个函数,并且您正在使用调度该函数this.props.dispatch(clicking(isClicking));。如果要保留动作的嵌套结构,则应修改this.props.dispatch(clicking(isClicking)());自动调用从clicking动作接收的函数的分派。
但是,建议的用途是修改您的clicking操作...
export function clicking(isClicked) {
dispatch({ type: "CLICK_OPEN", isClicked });
}
Run Code Online (Sandbox Code Playgroud)
请记住,您可以将商店导入到动作文件中,并用于store.dispatch调度动作。您不需要dispatch从组件传递函数。
| 归档时间: |
|
| 查看次数: |
19900 次 |
| 最近记录: |