score:1
This is the usual structure of an async
action to a reducer, but using typescript.
Basically you define the data types as interfaces
on the top, those are just objects defining what kind of data you're expecting to pass through.
Then, you have the action and the reducer.
The implementation of these is exactly the same as regular react-redux. However, you are implementing an async
action (making an asynchronous call using fetch
), so you also need to use redux-thunk
and its type from typescript, AppThunkAction
.
Therefore, you need to specify what 'thing' (type of data) every function is expecting to pass.
In case you need a particular data type (complex type), it's convenient to create an interface
for it. Otherwise, you can repeat this complex type following the columns (:
) of every object you want to assign it, but this is not recommended.
For example, if I'm expecting a object from an API call with id
, name
, hobbies
and timestamp
, an interface for that complex type would be:
export interface dataFromAPICall {
id: number;
name: string;
hobbies?: string[];
timestamp: number;
}
In this case, hobbies is optional (?
) and it expects and array of strings ['football', 'sky', 'bodyboard surfing']
.
In case of complex types on popular libraries, it is common that those types are already defined, such as AppThunkAction
in redux, or ReactNode
in React.
Hope this helps!
Source: stackoverflow.com
Related Query
- Can someone please explain the reducer function of this reducer function header?
- Can someone please explain me this code from ReactJS?
- Can someone explain the createStore function and how just passing the counter function to it makes it aware of the state?
- can someone explain to me how the async works in this scenario of useEffect?
- Can someone please explain how all the formatting tools works in VS Code?
- Can someone explain me the difference between both this if conditions in React?
- Can someone explain to me why one function works and the other don't?
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component. please help me to solve this error
- Can anyone explain why this returns between 50 to 100 of the same values? I am using this to change what a function does at a certain window width
- Can someone explain why this react component is not outputting the images or the correct key prop?
- Can someone explain the 'super()' keyword in ES6 Javascript (especially related to React)?
- Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons
- Can someone explain this to me from event loop perspective?
- How to update specific value in a array of JSON values in React, can anyone please explain this
- React-Redux: Copying an array from the state in Reducer function so that you can modify it
- Please explain the use of spread operator in this case
- How can I add a CORS header to this Lambda function
- How can i hardcode the date in this function using next js
- TypeError: Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function
- Can someone give a more real-world example where the setState(updaterFunc) function is more useful than the setState(object)?
- Can someone explain this react native code snippet to me?
- Can someone explain in the most layman terms possible what unmountComponentAtNode' does?
- Can someone explain what this line is doing
- Getting this error "Invalid hook call. Hooks can only be called inside of the body of a function component." inside a react functional component?
- Why I'm getting this Error: Invalid hook call. Hooks can only be called inside of the body of a function component/
- React/JavaScript, can someone explain why setState doesn't seem to correctly update the state during the function?
- Can someone explain what is the best way to share an URL that can keep track of the Redux state?
- Reactjs : Can someone please elaborate this line to me
- React Tic Tac Toe tutorial: can you explain how a number is passed as an argument to the function of the click event?
- Please explain this syntax of function signature in reactjs
More Query from same tag
- How to override Box created by TabPanel in Material UI
- How do I prevent keypress eventListner from setting my state variable to true then false?
- How to customize BotFramework webchat react UI
- Screen recordings in JavaScript with navigator.mediaDevices.getDisplayMedia aren't seekable
- Trying to get Button to copy URL and render snackbar using React Hooks
- How to redirect/route to different link upon smaller screen resolution and vice versa?
- React: Replace multiple, different parts of a html string with JSX components
- Link is not working in react-router-dom in version 5.3.0. Please anyone resolve this issue
- How do I pass a variable on the graphql filter?
- how to to insert TradingView widget into react js which is in script tag link: https://www.tradingview.com/widget/market-overview/
- Relay.js - partial fetch when conditional statement changes
- Gatsby + Reach router: best pattern for controlling client route paths
- select onchange event.value is undefined
- Material table for react font size is way too small
- React Hooks Auth0-js Context Hook Not Updating
- Initial store is loaded when store is deleted from local storage redux-persist
- react - Difference between createMuiTheme and createTheme
- React Redux: Delay between component inner content while updating
- Type error on vsc but still can be complied?
- how to update/re-render a React list after an item is deleted, using classes
- TypeError: Cannot read property of undefined after page re-renders
- How to differentiate context values if their state changed in React useEffect
- React Hooks: Uncaught TypeError: setData({...}) is not a function
- Changing state based off result of helper function in component
- Invalid hook call with useCallback in react
- How can I have only one checkbox checked out of two?
- How can I use CSS Pseudo-elements styling inside React JSX to pass variables?
- How to use Props with Generics with React.memo
- i want only admin to route and users to restrict
- Error: A React component suspended while rendering, but no fallback UI was specified