So here is my problem: I fetch an API, here is the action:
const OPTIONS = {
method: 'GET',
headers: {
'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
'X-RapidAPI-Key': '44316d2130msh21fed66e06e6a24p1dd597jsnf2e92ca6ac85'
}
};
export function setLeagues() {
const countries =
[
{
france: ["Ligue 1", "Ligue 2"]
},
{
england: ["Premier League"]
},
{
germany: ["Bundesliga 1"]
}
];
let leagues = [];
countries.forEach((country) => {
fetch(`https://api-football-v1.p.rapidapi.com/v2/leagues/current/${Object.keys(country)[0]}`, OPTIONS)
.then(response => response.json())
.then((data) => {
Object.values(country)[0].forEach((league) => {
data.api.leagues.filter((league_api) => {
if(league_api.name === league) {
leagues.push(league_api);
}
});
});
});
});
return {
type: 'SET_LEAGUES',
payload: leagues
};
}
The leagues return a correct array of league's object. Here is the reducer :
export default function(state, action) {
if (state === undefined) {
return [];
}
if (action.type === 'SET_LEAGUES') {
return action.payload;
} else {
return state;
}
}
And finally my container :
class LeaguesLabel extends Component {
componentDidMount() {
this.props.setLeagues()
}
render() {
console.log(this.props.leagues);
return(
<div>
<ul>
{
this.props.leagues.map((league) => {
return(
<li>
{league.name}
</li>
);
})
}
</ul>
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{ setLeagues: setLeagues },
dispatch
);
}
function mapStateToProps(state) {
return {
leagues: state.leagues
}
}
export default connect(mapStateToProps, mapDispatchToProps)(LeaguesLabel);
Of course I import setLeague into this container.
Nothing is displayed while my console.log(this.props.leagues) displayed a good Array of object who contain a "league".
thank you in advance for your help !