R
R
rsoinvi2018-06-19 21:27:19
redux
rsoinvi, 2018-06-19 21:27:19

How to dispatch data at any time?

Tell me how can I send the data right away?
In my example, I will export the fetchWeatherData() function and dispatch to index.js via store.dispatch(fetchWEatherData())
How can I immediately dispatch the data to the store?

import { connect } from 'react-redux';
import {
  fetchWeatherDataSuccess,
  fetchWeatherDataError
} from '../reducers/actions/weatherDataActions';

const name = "kiev";
const fetchWeatherDataBegin = () => {
  const url = "http://api.openweathermap.org/data/2.5/forecast?q="+ name +"&units=metric&APPID=8487f3a01e2983e87c74a057c147fa92";
  return fetch(url);
}

export const fetchWeatherData = () => dispatch => {
    fetchWeatherDataBegin().then(
      response => response.json()
      .then(
        data => dispatch(fetchWeatherDataSuccess(data)),
        error => dispatch(fetchWeatherDataError(error)),
      )
    )
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question