us

Chancellor: 'Tough times' as coronavirus affects UK economy

The chancellor says there have already been "tough times" as the coronavirus outbreak has an impact on the UK and warns "there will be more to come".




us

Coronavirus: 'My cafe's going bust before it's even opened'

A car factory worker turned cafe owner explains how coronavirus is affecting his business dream.




us

Coronavirus: Aer Lingus flight had 'no social distancing' says passenger

Sean Mallon's photos of an Aer Lingus Belfast-Heathrow flight showed passengers sitting close together.




us

Coronavirus: Bread and cake tips from a self-isolating baker

Ray normally runs his family bakery, Rinkoffs, but is currently staying at home with his wife.




us

Coronavirus: Three continents, four lives, one day

The stories of people who died on one day, from an exile who returned home to a disaster survivor.




us

Coronavirus coffee farmer: 'We're definitely scared'

Many small coffee producers fear they will go under, as Covid-19 has shut down their usual buyers.




us

Coronavirus: Disease meets deforestation at heart of Brazil's Amazon

Coronavirus has overwhelmed Manaus, the Amazon's biggest city, and the worst is yet to come.




us

Mexico receives ventilator shipment from US

The 211 machines were purchased from a US firm, Mexico's foreign minister said.




us

Life for asylum seekers in lockdown on the US-Mexico border

Magaly Contreras has spent nine months in a Tijuana shelter and is worried about her future.




us

Coronavirus: São Paulo governor at odds with Bolsonaro

São Paulo Governor João Doria has imposed tough virus curbs, a move slammed by President Bolsonaro.




us

Coronavirus: Brazil's outbreak 'threatens Paraguay's success'

Paraguay's president says he has reinforced the border with the worst-hit country in South America.




us

Using Proxies with Redux Types

One of the most common problems that I run into when using Redux is trying to figure out why an action is not being captured by a reducer. For someone just getting starting with Redux, debugging this issue can be especially overwhelming because of how Redux manages data flow. So before you start pouring over configuration code, or the logic contained in your action creators and reducers, please, make sure your action types are defined and spelled correctly.

One of the most common problems that I run into when using Redux is trying to figure out why an action is not being captured by a reducer. For someone just getting starting with Redux, debugging this issue can be especially overwhelming because of how Redux manages data flow. So before you start pouring over configuration code, or the logic contained in your action creators and reducers, please, make sure your action types are defined and spelled correctly.

In any application that I have built, most bugs that I have run into are simply due to typos. However, the solution to this particular problem is harder to spot because no errors are raised when the application is run. Take a look at the snippet below.

// actionTypes.js

export const FETCH_FILE_REQUEST = 'fetch_file_request';
export const FETCH_FILE_SUCCESS = 'fetch_file_success';
export const FETCH_FILE_FAIL = 'fetch_file_fail';


// filesReducer.js

import {
  FETCH_FILE_REQUEST,
  FETCH_FILE_SUCESS,
  FETCH_FILE_FAIL
} from '../actions/actionTypes';

const filesReducer = (state = {}, action) => {
  switch (action.type) {
    case FETCH_FILE_SUCESS:
      return { ...state, file: action.payload };
    default:
      return state;
  }
}

export default filesReducer;

Assuming we dispatched an action with type FETCH_FILE_SUCCESS, the filesReducer should catch the action before the default case is returned. But what if that is not happening? Where do we start the debugging process. There does not appear to be anything wrong with the code in the reducer; the action type was imported and matches the case in the switch statement. There are no errors in the browser. Where is the issue?

You may have noticed that I misspelled SUCCESS in filesReducer.js, but the reason this can be hard to catch is because importing undefined types does not cause an error, so when we import FETCH_FILE_SUCESS, its value is actually undefined, so our reducer always hits the default case.

It would be nice if the existing import/export system could help us catch this. Unfortunately, since action types are just strings, validating their existence is challenging. Luckily, we have another option.

Enter Proxies

Proxies are a feature of ES2015 that allow us to customize operations on a object. They can be used in many different ways, and you can find some useful examples here and here. For our problem, this example from Mozilla looks promising:

let validator = {
  set: function(obj, prop, value) {
    if (prop === 'age') {
      if (!Number.isInteger(value)) {
        throw new TypeError('The age is not an integer');
      }
      if (value > 200) {
        throw new RangeError('The age seems invalid');
      }
    }

    // The default behavior to store the value
    obj[prop] = value;

    // Indicate success
    return true;
  }
};

let person = new Proxy({}, validator);

person.age = 100;
console.log(person.age); // 100
person.age = 'young'; // Throws an exception
person.age = 300; // Throws an exception

So if proxies can be used to validate that properties assigned to an object are of a certain type and value, we should definitely be able to ensure that our action types are never undefined, or else throw an error that will be easy for us to fix. Let’s refactor our actionTypes.js file.

// actionTypes.js

const types = {
  FETCH_FILE_REQUEST: 'fetch_file_request',
  FETCH_FILE_SUCCESS: 'fetch_file_success',
  FETCH_FILE_FAIL: 'fetch_file_fail'
}

const typeValidator = {
  get(obj, prop) {
    if (obj[prop]) {
      return prop;
    } else {
      throw new TypeError(`${prop} is not a valid action type`);
    }
  }
}

module.exports = new Proxy(types, typeValidator);

First, we define a object containing all our action types. Then we define our validator handler typeValidator. The get method inside our handler is called a trap, and provides access to the properties of a object. If the property we are looking for, an action type, in this case, exists in the types object, return that prop, unmodified. Otherwise, throw an error because the prop does not exist.

Finally, export a new proxy, passing the types object as the target and the typeValidator as the handler. However, it is important to note that the ES2015 module system does not work well with proxies, so module.exports and require() must be used for exporting and importing the types.

Barely any code needs to change in the reducer and action creator files, but in order for the action types to be imported successfully, we just need couple lines of code in a new file:

// actionTypesProxy.js

export const {
  FETCH_FILE_REQUEST,
  FETCH_FILE_SUCCESS,
  FETCH_FILE_FAIL,
} = require('./actionTypes');

// in the reducer and action creator files
// change '../actions/actionTypes' to
// '../actions/actionTypesProxy'

By creating a proxy to verify the existence of an action type, we no longer have to worry about correctly naming a property upon import because an error will be thrown in the browser console as soon as the application starts. So, reduce the number headaches you get when developing an application using Redux and start using proxies.

Interested in learning how to build applications using Redux with ReactJS. Check out this online course! Modern React with Redux




us

Paulinho of Brazil battles for the ball with goalkeeper Youssouf Koita of Mali

KOLKATA, INDIA - OCTOBER 28: Paulinho (C) of Brazil battles for the ball with goalkeeper Youssouf Koita of Mali during the FIFA U-17 World Cup India 2017 3rd Place match between Brazil and Mali at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Buda Mendes - FIFA/FIFA via Getty Images)




us

The small things for Russia’s Lima

Sometimes in life it is the smallest things that have the biggest consequences. In the case of Russia forward Eder Lima, it was a simple red card that led to him preparing to face Iran in the semi-final of the FIFA Futsal World Cup Colombia 2016 on Tuesday. 




us

Russia outlast Iran to make final

Russia booked their place in the FIFA Futsal World Cup Colombia 2016 final after outlasting Iran 4-3 in Tuesday’s semi-final in Medellin.




us

Javid caught between pride and frustration

FIFA.com ​spoke to the disconsolate yet proud Mahdi Javid after Iran were defeated 4-3 by Russia in the semi-finals of the FIFA Futsal World Cup.




us

Coelho: No excuses, Argentina deserved it

Bruno Coelho speaks to ​FIFA.com ​after his side fell at the final hurdle, defeated by Argentina in the semi-finals of the FIFA Futsal World Cup Colombia 2016.




us

One final push for pride and glory

However the FIFA Futsal World Cup Colombia 2016 concludes, the tournament will go down as a watershed. For the first time, the trophy will not be lifted by Brazil or Spain, with either Russia or Argentina set to claim the crown in Cali.




us

Seven questions to Skorovich and Giustozzi

To lift the FIFA Futsal World Cup, teams must successfully negotiate seven matches, as Russia and Argentina – the Colombia 2016 finalists who will lock horns on Saturday – know all too well. Prior to that decisive duel, FIFA.com posed seven questions to the coaches of the two surviving teams. 

 




us

Russia, Portugal, Spain and Kazakhstan secure Futsal World Cup returns




us

Sanchez and Monterrey eye victorious 2019 finale




us

Mahdi Javid (R) of Iran and Robinho (L) of Russia fight for the ball

MEDELLIN, COLOMBIA - SEPTEMBER 27: Mahdi Javid (R) of Iran and Robinho (L) of Russia fight for the ball during the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Dmitry Lyskov of Russia celebrates his goal

MEDELLIN, COLOMBIA - SEPTEMBER 27: Dmitry Lyskov of Russia celebrates his goal during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Hossein Tayebi of Iran battles with Vladislav Shayakhmetov of Russi

MEDELLIN, COLOMBIA - SEPTEMBER 27: Hossein Tayebi of Iran battles with Vladislav Shayakhmetov of Russia during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Sergey Abramov of Russia celebrates his goal

MEDELLIN, COLOMBIA - SEPTEMBER 27: Sergey Abramov of Russia celebrates his goal with team mates during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Sergey Abramov of Russia celebrates his goal

MEDELLIN, COLOMBIA - SEPTEMBER 27: Sergey Abramov of Russia celebrates his goal with team mates during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Mehran Alighadr (L) of Iran is brought down by Ivan Milovanov (R) of Russia

MEDELLIN, COLOMBIA - SEPTEMBER 27: Mehran Alighadr (L) of Iran is brought down by Ivan Milovanov (R) of Russia during the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Vladislav Shayakhmetov (R) of Russia shoots on goal and scores

MEDELLIN, COLOMBIA - SEPTEMBER 27: Vladislav Shayakhmetov (R) of Russia shoots on goal and scores his team's third goal during the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Vladislav Shayakhmetov (C) of Russia celebrates

MEDELLIN, COLOMBIA - SEPTEMBER 27: Vladislav Shayakhmetov (C) of Russia celebrates next to his teammates Sergey Abramov (L) and Dmitry Lyskov (2nd L) after scoring his team's third goal during the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Hossein Tayebi (R) of Iran and Danil Davydov (L) of Russia compete for the ball

MEDELLIN, COLOMBIA - SEPTEMBER 27: Hossein Tayebi (R) of Iran and Danil Davydov (L) of Russia compete for the ball during the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Players of Russia celebrate at the end of the semi final against Iran

MEDELLIN, COLOMBIA - SEPTEMBER 27: Players of Russia celebrate at the end of the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Danil Davydov of Russia celebrates at the end of the

MEDELLIN, COLOMBIA - SEPTEMBER 27: Danil Davydov of Russia celebrates at the end of the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Sergey Abramov (C) of Russia celebrates at the end of the semfinal againstl Iran

MEDELLIN, COLOMBIA - SEPTEMBER 27: Sergey Abramov (C) of Russia celebrates at the end of the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Eder Lima of Russia offers a signed shirt to a fan after

MEDELLIN, COLOMBIA - SEPTEMBER 27: Eder Lima of Russia offers a signed shirt to a fan after the final whistle during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Eder Lima of Russia takes a selfie with spectators

MEDELLIN, COLOMBIA - SEPTEMBER 27: Eder Lima of Russia takes a selfie with spectators after the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Gustavo of Russia poses for a selfie with fans

MEDELLIN, COLOMBIA - SEPTEMBER 27: Gustavo of Russia poses for a selfie with fans after the final whistle during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Eder Lima of Russia competes for the bal

MEDELLIN, COLOMBIA - SEPTEMBER 27: Eder Lima of Russia competes for the ball during the FIFA Futsal World Cup semi-final match between Iran and Russia at Coliseo Ivan de Bedout on September 27, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Sergey Abramov of Russia takes a selfie with spectators

MEDELLIN, COLOMBIA - SEPTEMBER 10: Sergey Abramov of Russia takes a selfie with spectators after the FIFA Futsal World Cup Group B match between Thailand and Russia at Coliseo Ivan de Bedout stadium on September 10, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Sergey Abramov of Russia takes a selfie with spectators

MEDELLIN, COLOMBIA - SEPTEMBER 10: Sergey Abramov of Russia takes a selfie with spectators after the FIFA Futsal World Cup Group B match between Thailand and Russia at Coliseo Ivan de Bedout stadium on September 10, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Players of Russia huddle prior to the FIFA Futsal World Cup Group B match between Thailand and Russia

MEDELLIN, COLOMBIA - SEPTEMBER 10: Players of Russia huddle prior to the FIFA Futsal World Cup Group B match between Thailand and Russia at Coliseo Ivan de Bedout stadium on September 10, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Vladislav Shayakhmetov of Russia looks on from the tunnel

CALI, COLOMBIA - SEPTEMBER 16: Vladislav Shayakhmetov of Russia looks on from the tunnel during the FIFA Futsal World Cup Group B match between Russia and Cuba at Coliseo el Pueblo on September 16, 2016 in Cali, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Players shake hand at the end of the match during the FIFA Futsal World Cup round of 16 match between Russia and Vietnam

MEDELLIN, COLOMBIA - SEPTEMBER 20: Players shake hand at the end of the match during the FIFA Futsal World Cup round of 16 match between Russia and Vietnam at Coliseo Ivan de Bedout on September 20, 2016 in Medellin, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

The Russian dressing room is seen ahead of the FIFA Futsal World Cup Quarter-Final match between Russia and Spain

CALI, COLOMBIA - SEPTEMBER 24: The Russian dressing room is seen ahead of the FIFA Futsal World Cup Quarter-Final match between Russia and Spain at the Coliseo el Pueblo Stadium on September 24, 2016 in Cali, Colombia. (Photo by Ian MacNicol - FIFA/FIFA via Getty Images)




us

Eder Lima of Russia gives a shirt of Russia to a young blind spectator

MEDELLIN, COLOMBIA - SEPTEMBER 27: Eder Lima of Russia gives a shirt of Russia to a young blind spectator after the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

Head Coach Diego Giustozzi of Argentina and his coaching staff stand

BUCARAMANGA, COLOMBIA - SEPTEMBER 22: Head Coach Diego Giustozzi of Argentina and his coaching staff stand for the playing of their national anhtem prior to round of 16 match play between Argentina and Ukraine in the 2016 FIFA Futsal World Cup at Coliseo Bicentenario on September 22, 2016 in Bucaramanga, Colombia. Argentina defeated Ukraine 1-0 in extra time. (Photo by Victor Decolongon - FIFA/FIFA via Getty Images)




us

Eder Lima of Russia gives a shirt of Russia to a young blind spectator

Eder Lima of Russia gives a shirt of Russia to a young blind spectator after the FIFA Futsal World Cup Semi-Final match between Iran and Russia at Coliseo Ivan de Bedout stadium on September 27, 2016 in Medellin, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




us

A Montage showing Sergey Skorovich, coach of Russia, and Diego Giustozzi, coach of Argentina, at the FIFA Futsal World Cup Colombia 2016

A Montage showing Sergey Skorovich, coach of Russia, and Diego Giustozzi, coach of Argentina, at the FIFA Futsal World Cup Colombia 2016




us

Diego Giustozzi the coach of Argentina is seen in the tunnel

Diego Giustozzi the coach of Argentina is seen in the tunnel prior to the FIFA Futsal World Cup Final match between Russia and Argentina at the Coliseo el Pueblo Stadiumon October 1, 2016 in Cali, Colombia. (Photo by Ian MacNicol - FIFA/FIFA via Getty Images)




us

Russia and Argentina line up for the national anthems

CALI, COLOMBIA - OCTOBER 01: Teams line up for the national anthems during the FIFA Futsal World Cup final between Russia and Argentina at Coliseo el Pueblo on October 1, 2016 in Cali, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




us

Teams line up for the national anthems during the FIFA Futsal World Cup final between Russia and Argentina

Teams line up for the national anthems during the FIFA Futsal World Cup final between Russia and Argentina at Coliseo el Pueblo on October 1, 2016 in Cali, Colombia. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)