pe

The rapper's track that sparked a wave of killings

Tensions have long existed between gangs in Tottenham and Wood Green - for 10 weeks in 2018 they boiled over.




pe

Electrosensitivity: 'I didn't believe people had it, then it happened to me'

Velma, Emma and Dean believe mobile phone signals, wi-fi and other modern technology makes them ill.




pe

Coronavirus career pivots: 'I now work in a supermarket'

An actress and a commercial sales leader talk about making the switch to working in a supermarket.




pe

Coronavirus: Three perspectives on furloughing

Three people react to the furloughing scheme put in place in response to the coronavirus outbreak.




pe

Coronavirus: 'My parents' campervan has become my office'

A marketing manager explains why she turned a campervan into her office during coronavirus.




pe

Coronavirus: Rising commercial PPE costs 'frustrating', says care home CEO

The CEO of Methodist Homes says a secure supply chain from government would mean avoiding inflated prices.




pe

Coronavirus: The unexpected items deemed 'essential'

Cheese shops and gun stores are among the services still open in locked down places around the world.




pe

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.




pe

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




pe

Al Ain 3-3 (4-3 pens) Team Wellington (UAE 2018)

A penalty shootout decided the opening match of the FIFA Club World Cup UAE 2018. Hosts Al Ain knocked out OFC champions Team Wellington thanks to the heroics of Al Ain goalkeeper Khalid Eisa.




pe

Esperance 1-1 (6-5 pens) CD Guadalajara (UAE 2018)

Nine-man Esperance ended their FIFA Club World Cup UAE 2018 campaign on a high after a tense 6-5 penalty shoot-out win against Chivas in the match for fifth place.




pe

River Plate 2-2 (4-5 pens) Al Ain (UAE 2018)

Hosts Al Ain secured a place in the final, defeating the South American champions on penalties.




pe

Javier Ceppi, Praful Patel, and Jaime Yarza speak to the media during a press conference

KOLKATA, INDIA - OCTOBER 26: L-R: Javier Ceppi, Tournament Director at LOC FIFA U-17 World Cup India 2017, Patel Praful and Head of FIFA Tournaments, Jaime Yarza speak to the media during a press conference ahead of the FIFA U-17 World Cup India 2017 tournament at Vivekananda Yuba Bharati Krirangan on October 26, 2017 in Kolkata, India. (Photo by Tom Dulat - FIFA/FIFA via Getty Images)




pe

Javier Ceppi, Praful Patel and Jaime Yarza speak during a India 2017 press conference

KOLKATA, INDIA - OCTOBER 26: L-R: Javier Ceppi, Tournament Director at LOC FIFA U-17 World Cup India 2017, Patel Praful and Head of FIFA Tournaments, Jaime Yarza speak to the media during a press conference ahead of the FIFA U-17 World Cup India 2017 tournament at Vivekananda Yuba Bharati Krirangan on October 26, 2017 in Kolkata, India. (Photo by Tom Dulat - FIFA/FIFA via Getty Images)




pe

Praful Patel and Jaime Yarza speak to the media during an India 2017 press conference

KOLKATA, INDIA - OCTOBER 26: Patel Praful and Head of FIFA Tournaments, Jaime Yarza speak to the media during a press conference ahead of the FIFA U-17 World Cup India 2017 tournament at Vivekananda Yuba Bharati Krirangan on October 26, 2017 in Kolkata, India. (Photo by Tom Dulat - FIFA/FIFA via Getty Images)




pe

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)




pe

Goalkeeper of Brazil, Gabriel Brazao in action

KOLKATA, INDIA - OCTOBER 28: Goalkeeper of Brazil, Gabriel Brazao in action 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 Tom Dulat - FIFA/FIFA via Getty Images)




pe

Goalkeeper of Brazil, Gabriel Brazao in action

KOLKATA, INDIA - OCTOBER 28: Goalkeeper of Brazil, Gabriel Brazao in action 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 Tom Dulat - FIFA/FIFA via Getty Images)




pe

Goalkeeper of Brazil, Gabriel Brazao celebrates

KOLKATA, INDIA - OCTOBER 28: Goalkeeper of Brazil, Gabriel Brazao celebrates after 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 Tom Dulat - FIFA/FIFA via Getty Images)




pe

Sergio Gomez of Spain scores the opening goal

KOLKATA, INDIA - OCTOBER 28: Sergio Gomez of Spain scores the opening goal during the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Tom Dulat - FIFA/FIFA via Getty Images)




pe

Coach of England, Steve Cooper looks on

KOLKATA, INDIA - OCTOBER 28: Coach of England, Steve Cooper looks on prior to the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Tom Dulat - FIFA/FIFA via Getty Images)




pe

Head coach Steve Cooper of England gives a gift for coach Santiago Denia of Spain

KOLKATA, INDIA - OCTOBER 28: Head coach Steve Cooper of England gives a gift for coach Santiago Denia of Spain during the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Buda Mendes - FIFA/FIFA via Getty Images)




pe

England's midfielder Phil Foden greets spectators

England's midfielder Phil Foden greets spectators after England's win over Spain in the final FIFA U-17 World Cup football match at the Vivekananda Yuba Bharati Krirangan stadium in Kolkata on October 28, 2017. / AFP / Dibyangshu SARKAR




pe

England's midfielder Phil Foden greets spectators after England's win over Spain

England's midfielder Phil Foden greets spectators after England's win over Spain in the final FIFA U-17 World Cup football match at the Vivekananda Yuba Bharati Krirangan stadium in Kolkata on October 28, 2017. / AFP / Dibyangshu SARKAR




pe

Phil Foden (R) with his 'Man of the Tournament' trophy, Rhin Brewster (L) with his 'Highest Scorer' trophy of England and goalkeeper Gabriel Brazao with his 'Best Goalkeeper' trophy pose

Phil Foden (R) with his 'Man of the Tournament' trophy, Rhin Brewster (L) with his 'Highest Scorer' trophy of England and goalkeeper Gabriel Brazao with his 'Best Goalkeeper' trophy pose for a photo after the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Buda Mendes - FIFA/FIFA via Getty Images)




pe

England manager Steve Cooper celebrates after the 5th goal

KOLKATA, INDIA - OCTOBER 28: England manager Steve Cooper celebrates after the 5th goal during the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




pe

England manager Steve Cooper celebrates with the trophy

KOLKATA, INDIA - OCTOBER 28: England manager Steve Cooper celebrates with the trophy during the FIFA U-17 World Cup India 2017 Final match between England and Spain at Vivekananda Yuba Bharati Krirangan on October 28, 2017 in Kolkata, India. (Photo by Jan Kruger - FIFA/FIFA via Getty Images)




pe

Iran finish third after penalty shootout win

Iran’s remarkable journey at the FIFA Futsal World Cup Colombia 2016 ended on a high note, as they claimed third place after a 4-3 penalty shootout win over Portugal following a 2-2 draw in Cali on Saturday.




pe

Futsal's road to Lithuania mapped out




pe

First four of six European World Cup places up for grabs




pe

South American hopefuls seeking Lithuania berths




pe

Suarez at the fore as Venezuelans eye landmark appearance

World Cup-bound Suarez flourishing in Finland




pe

Filipe Luis: We're giving the semi-final maximum priority




pe

Reds return as Rayados hope it's their time




pe

Elhouni: Esperance’s centenary has been special




pe

Dalian youngsters enjoy thrilling Wanda FIFA Flag Bearer experience




pe

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)




pe

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)




pe

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)




pe

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)




pe

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)




pe

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)




pe

Alan Brandi (C) of Argentina competes for the ball with Joao Matos (L) and Ricardinho (R) of Portugal

Alan Brandi (C) of Argentina competes for the ball with Joao Matos (L) and Ricardinho (R) of Portugal during the FIFA Futsal World Cup Semi-Final match between Argentina and Portugal at the Coliseo El Pueblo stadium on September 28, 2016 in Cali, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




pe

Cristian Borruto (L) of Argentina scores the opening goal

Cristian Borruto (L) of Argentina scores the opening goal during the FIFA Futsal World Cup Semi-Final match between Argentina and Portugal at the Coliseo El Pueblo stadium on September 28, 2016 in Cali, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




pe

Cristian Borruto (L) of Argentina scores the opening goal

Cristian Borruto (L) of Argentina scores the opening goal during the FIFA Futsal World Cup Semi-Final match between Argentina and Portugal at the Coliseo El Pueblo stadium on September 28, 2016 in Cali, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




pe

Goalkeeper Nicolas Sarmiento #1 of Argentina is congratulated by fellow keeper Guido Mosenson

BUCARAMANGA, COLOMBIA - SEPTEMBER 12: Goalkeeper Nicolas Sarmiento #1 of Argentina is congratulated by fellow keeper Guido Mosenson after their 1-0 Group E match win against Kazakhstan in the 2016 FIFA Futsal World Cup on September 12, 2016 in Bucaramanga, Colombia. (Photo by Victor Decolongon - FIFA/FIFA via Getty Images)




pe

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)




pe

Cristian Borruto of Argentina celebrates after he scores the opening goal

CALI, COLOMBIA - SEPTEMBER 28: Cristian Borruto of Argentina celebrates after he scores the opening goal during the FIFA Futsal World Cup Semi Final match between Argentina and Portugal at the Coliseo el Pueblo Stadium on September 28, 2016 in Cali, Colombia. (Photo by Ian MacNicol - FIFA/FIFA via Getty Images)




pe

Pedro Cary of Portugal is brought down by Mehran Alighadr of Iran

CALI, COLOMBIA - OCTOBER 01: Pedro Cary of Portugal is brought down by Mehran Alighadr of Iran during the FIFA Futsal World Cup Third Place Play off match between Iran and Portugal at the Coliseo El Pueblo stadium on October 1, 2016 in Cali, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)




pe

Pedro Cary (R) of Portugal and Hossein Tayebi (L) of Iran fight for the ball

Pedro Cary (R) of Portugal and Hossein Tayebi (L) of Iran fight for the ball during the FIFA Futsal World Cup Third Place Play off match between Iran and Portugal at the Coliseo El Pueblo stadium on October 1, 2016 in Cali, Colombia. (Photo by Alex Caparros - FIFA/FIFA via Getty Images)