繁体   English   中英

减速器看不到一个特定的动作[react-redux]

[英]Reducer can't see one particular action [react-redux]

你是我最后的希望。 我现在很沮丧

我刚刚将所有项目文件都放入了Github

这是我的问题。 在index.js文件中的actions文件夹内,我有两个功能:

fetchLocation()fetchWeather()

当用户输入城市时,我的应用正在使用fetchLocation调度操作,并正在从Google Maps Api获取数据。 ReduxPromise中间件将Promise更改为对象,并且一切正常。 我可以使用这些数据通过Google地图在网站上显示该城市。

现在我有了lat&lng,所以我将该数据与fetchWeather(lat,lng)一起使用 在actions / index.js内部,我的fetchWeather()函数正在运行,因为我在console.log中看到了数据。 使用新的纬度和纬度,它可以从api.wunderground获取具有天气预报的新数据。 而且我可以在console.log和request2里面看到这些数据。

这是此错误/奇怪的行为。 fetchWeather永远不会像fetchLocation那样表现。 我再也没有回来

  return {
    type: FETCH_WEATHER,
    payload: request2
  };

reducer_weather.js永远不会用开关案例FETCH_WEATHER触发,而且我从未见过console.log('INSIDE!')。

我正在使用ReduxDevTootls,我的应用程序所做的所有事情都是通过位置减少器更新状态,因此我可以拥有多张地图,但是reducer_weather始终保持沉默。

你能告诉我为什么吗?

这是我来自action / index.js的代码。 我的其余代码可以在这里找到: Github

import axios from 'axios';
export const FETCH_LOCATION = 'FETCH_LOCATION';
export const FETCH_WEATHER = 'FETCH_WEATHER';
const API_KEY_GOOGLE = 'AIzaSyDNMmI2f7qIcBnKgV1dYXmi995BY_8zoJM';
const API_KEY_WUNDERGROUND = 'cd8c7ea98a37877f';

export function fetchLocation(city) {
  const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${city}&key=${API_KEY_GOOGLE}`
  const request = axios.get(url);
  console.log('request1: ', request);
  return {
    type: FETCH_LOCATION,
    payload: request
  };
}

export function fetchWeather(lat, lng) {
  console.log(lat, lng);

  const url = `https://api.wunderground.com/api/${API_KEY_WUNDERGROUND}/forecast10day/q/${lat},${lng}.json`;

  console.log(url);

  const request2 = axios.get(url);

  console.log('request2: ', request2);

  return {
    type: FETCH_WEATHER,
    payload: request2
  };
}

在此处输入图片说明

您必须dispatch动作。 但是,您只是将动作生成器称为普通函数。

从您的仓库中替换

fetchWeather(lat, lng);

this.props.fetchWeather(lat, lng);

由于您的actionCreators通过Redux Connectdispatch绑定。

更新: this不适用于Component类的用户定义函数(不是默认的生命周期方法)。 因此,使用的脂肪箭头功能es6访问this里面自己的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM