External Request

Use effect to handle external effects, mostly handel async calls.

Create a sink with effect to handle the external request

This is an example of using the effect to handle HTTP requests, using an async function

WeatherSink.js
import { sink, state, effect } from 'redux-sink';

@sink('weather')
export class WeatherSink {
  @state loading = false;
  @state weather = { 
    temperature: 0,
    humidity: 0
  };

  @effect
  async function loadWeather() {
    this.loading = true;
    // using async/await here, same for promise
    this.weather = await fetch('http://api/weather');
    this.loading = false; 
  }
}

Call effect load data in a component

Last updated

Was this helpful?