简体   繁体   English

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

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

You are my last hope. 你是我最后的希望。 I'm so frustrated right now... 我现在很沮丧

I've just put all my project files to Github . 我刚刚将所有项目文件都放入了Github

Here is my problem. 这是我的问题。 Inside actions folder in index.js file I have two functions: 在index.js文件中的actions文件夹内,我有两个功能:

fetchLocation() & fetchWeather() fetchLocation()fetchWeather()

When user inputs city my app is dispatching action with fetchLocation and is getting data from Google Maps Api. 当用户输入城市时,我的应用正在使用fetchLocation调度操作,并正在从Google Maps Api获取数据。 Promise is changed to object by ReduxPromise Middleware and everything works fine. ReduxPromise中间件将Promise更改为对象,并且一切正常。 I can use this data to show that city on website using Google maps. 我可以使用这些数据通过Google地图在网站上显示该城市。

Now I have lat & lng so I use that data with fetchWeather(lat, lng) . 现在我有了lat&lng,所以我将该数据与fetchWeather(lat,lng)一起使用 Inside actions/index.js my fetchWeather() function is working, because I see data in console.log. 在actions / index.js内部,我的fetchWeather()函数正在运行,因为我在console.log中看到了数据。 With new lat & lng it fetches new data from api.wunderground with weather forecast. 使用新的纬度和纬度,它可以从api.wunderground获取具有天气预报的新数据。 And I can see this data inside console.log and request2 is a promise. 而且我可以在console.log和request2里面看到这些数据。

And here is this bug/strange behavior. 这是此错误/奇怪的行为。 fetchWeather never behaves like fetchLocation did. fetchWeather永远不会像fetchLocation那样表现。 I never returns 我再也没有回来

  return {
    type: FETCH_WEATHER,
    payload: request2
  };

reducer_weather.js is never triggered with switch case FETCH_WEATHER, and I've never seen console.log('INSIDE!'). reducer_weather.js永远不会用开关案例FETCH_WEATHER触发,而且我从未见过console.log('INSIDE!')。

I'm using ReduxDevTootls and everything that my app does is updates states by location reducer so I can have multiple maps, but reducer_weather is always silent. 我正在使用ReduxDevTootls,我的应用程序所做的所有事情都是通过位置减少器更新状态,因此我可以拥有多张地图,但是reducer_weather始终保持沉默。

Could you please tell me why? 你能告诉我为什么吗?

Here is my code from actions/index.js. 这是我来自action / index.js的代码。 Rest of my code you can find here: Github . 我的其余代码可以在这里找到: 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
  };
}

在此处输入图片说明

You must dispatch the action. 您必须dispatch动作。 But you are simply calling the action generator as normal function. 但是,您只是将动作生成器称为普通函数。

Replace this line from your Repo 从您的仓库中替换

fetchWeather(lat, lng);

with

this.props.fetchWeather(lat, lng);

Since your actionCreators are binded with dispatch through Redux Connect . 由于您的actionCreators通过Redux Connectdispatch绑定。

UPDATE: this is not available for user defined functions(not default life cycle methods) of Component class. 更新: this不适用于Component类的用户定义函数(不是默认的生命周期方法)。 So use fat arrow functions of es6 to access this inside your own functions. 因此,使用的脂肪箭头功能es6访问this里面自己的函数。

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

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