score:1
Accepted answer
Dispatch a single action to update the store for result instead of dispatching multiple actions per field in the result.
src/actions/index.js
export const RECEIVE_TEAMS_STATS = "RECEIVE_TEAMS_STATS";
export const receivedTeamsStat = json => ({
type: RECEIVE_TEAMS_STATS,
json: json
});
export function getTeamsStats(league, team) {
return function(dispatch) {
return axios
.get(
`https://www.api-football.com/demo/api/v2/statistics/${league}/${team}`
)
.then(res => {
const {
wins: { home: teamsStatsWinHome, away: teamsStatsWinAway },
draws: { home: teamsStatsDrawHome, away: teamsStatsDrawAway },
loses: { home: teamsStatsLoseHome, away: teamsStatsLoseAway }
} = res.data.api.statistics.matchs;
const teamStats = {
teamsStatsWinHome,
teamsStatsWinAway,
teamsStatsDrawHome,
teamsStatsDrawAway,
teamsStatsLoseHome,
teamsStatsLoseAway
}
dispatch(receivedTeamsStat(teamStats));
})
.catch(e => {
console.log(e);
});
};
All 6 actions (RECEIVE_TEAMS_STATS_WIN_HOME
, RECEIVE_TEAMS_STATS_WIN_AWAY
, RECEIVE_TEAMS_STATS_DRAW_HOME
, RECEIVE_TEAMS_STATS_DRAW_AWAY
, RECEIVE_TEAMS_STATS_LOSE_HOME
, RECEIVE_TEAMS_STATS_LOSE_AWAY
) can be rolled into a RECEIVE_TEAMS_STATS
action and handled as follows:
src/reducers/index.js
import {
//..
RECEIVE_TEAMS_STATS
} from "../actions";
const reducer = (state = initialState, action) => {
switch (action.type) {
//...
case RECEIVE_TEAMS_STATS:
return {
...state,
...action.json,
isTeamsStatsLoading: false
};
//...
}
The benefit of dispatching a single update to the store is that Stats
component renders once as opposed to 6 times when results return from the API.
Source: stackoverflow.com
Related Query
- React Action Requests and Redux Structure
- Cannot read property '.then' of undefined when testing async action creators with redux and react
- Fractal Project Structure with React and Redux - pros/cons
- Calling an action from a different reducer with React and redux
- Can I send an AJAX call in React and Redux without action creators and reducers?
- Performance issues with a tree structure and shouldComponentUpdate in React / Redux
- React Redux folder structure for Fronted and Dashboard
- React Redux Fetch action returning "415 (Unsupported Media Type)" and "401 (unauthorized)"
- How action and reducers are connected with each other in react redux
- How to fetch data with multiple requests in Redux action creator and then send collected data to reducer?
- Using React and Redux Hooks, why is my action not firing?
- React redux multiple requests in one action creator
- React redux and initializing props with asynchronous action resolution
- React Hooks and Redux - trouble getting value from action creator
- React Redux: How to find last action called in Redux and fire that same action again
- REACT How to make AJAX requests with Redux and Axios (with promise middleware)?
- How to call action and set state in componentDidUpdate() in react and redux (prevent infinte loop)
- react with redux and react-router dispatches action twice
- How can I pass arguments in action different 3 level react components without redux and context API?
- React and Redux Shopping cart, add to cart action
- React Redux, my component is not connected to Redux Store. I tested the action and reducer in another component, it does work for other components?
- React and Redux toolkit : loose the state when I want to use filter action
- Async redux action to fetch data is causing component to reload and cause react to react max depth in reload
- How to run a timer in React component for 60 seconds and call a Redux action every 5 seconds
- Difficulties creating an action that fetches data with React Redux and axios
- dispatch action and redirect by calling function without page reload in react redux ( Page Reload Problem )
- unhandled rejection: action is undefined React js and Redux
- React Redux Call action within action and get return value
- keyof and React Redux action
- Using values from previously dispatched action in the next action in React and Redux
More Query from same tag
- React: validateDOMNesting: #text cannot appear as a child of <tr>
- React causing: Can't perform a React state update on an unmounted component
- Fetching local and production on Vercel
- Next.js SWR accessing data from cache
- Typescript IndexOf Issue in Array<String> useState
- CORS Error: LinkedIn Authentication; .NET Core 5 REST Api
- How to style react-select component so that it is inline with some text?
- Infinite loop when pushing into array using UseState()?
- React, ineffiencies of binding a new function for each event
- Using Ant Design Variables in Styled Components
- Typescript React Component get return value
- Submit a form when enter is pressed in a textarea in React?
- React - Redirect to a default path if path doesn't exist
- Uploading base64 to the firebase storage and getting back the download url
- React replace App component State Data with JSON
- Cannot use SocketIO without passing server uri to io() on the client-side
- Ant Design table row class name multiple conditions
- How do i use my incremented value inside inside an other component
- Easing animation using React and Styled Components
- Update state after axios POST request
- Image in Grid Item overflowing in React using Material UI
- React.js keep scroll at bottom
- How do I convert componentDidMount() and componentWillUnmount to the useEffect()?
- I need to call the page render after POST request
- Prettier doesn't format .tsx file
- Rendering React component from Redux state property
- Travis/Jest: TypeError: Cannot assign to read only property 'Symbol(Symbol.toStringTag)' of object '#<process>'
- How can i store the Ant Design's Time Picker time?
- How to read DraftJS state from localStorage?
- React state is updating without calling setState