I'm trying to make an app using API from https://swapi.co/
They have no pictures in their response so I thought to add them myself after fetching the data.
I'm fetching data from API in action like that
export const fetchCharacters = () => async dispatch => {
const response = await fetch('https://swapi.co/api/people/');
const data = await response.json();
const results = await data.results;
dispatch({
type: FETCH_CHARACTERS,
payload: results
});
}
After that in reduce
case FETCH_CHARACTERS :
return {
...state,
characters: action.payload
}
To load this data to my component:
componentDidMount() {
this.props.fetchCharacters();
}
After this this.props.characters is an array of objects.
So I've been thinking how can I add pictures for each character?
I'm thinking something like mapping over objects, adding new property to each one. And to assign right picture to character I'm thinking naming pictures 0.jpg, 1.jpg, and then in map function just doing somehing like:
character.pic = `link/${index}.jpg`;
Is it a good way of achieving this? Or is there a better way?
indexdoes not sound like a permanent property of each character. Maybe extract theidnumber from theurlproperty of each character.