score:1
you can just call the searchusers()
function in useeffect()
to get some users and set the state.
if you want to get initial users with some other logic you should probably write a different function and then call it when setting up the component.
const getinitialusers = async () => {
setloading();
let text = "blabla"; // or whatever your initial user query should look like modify the url below accordingly
const res = await axios.get(
`https://api.github.com/search/users?q=${text}&client_id=${githubclientid}&client_secret=${githubclientasecret}`
);
//dispatch to reducer object
dispatch({
type: search_users,
payload: res.data.items,
});
};
useeffect(()=>{
getinitialusers();
},[]);
score:0
the reasonable and suggest place to get your first async data is componentdidmount which in hook world it is translated to use effect with an empty array as its dependencies
const [state, dispatch] = usereducer(githubreducer, initialstate);
useeffect(() => {
searchusers('initaltextsearchfrompropsoranywhere')
}, [])
for more enhancement, you can call .then
to show snack bar and .catch
on to retry in case it fails for some reason and .finally
to set loading to false in both cases.
Source: stackoverflow.com
Related Query
- how to set the initialState with useReducer React
- I am trying to animate an object with css in react and change the animation (timing) dynamically, how can I set the style after animation end?
- React - how to set state with the data from a resolved promise
- How to set the params react router not misunderstand with the static router?
- How to setup a generic interface for the state when using React useReducer with typescript
- With a React Hooks' setter, how can I set data before the component renders?
- How to set the state of a component with the information obtained from a promise to avoid memory leaks in react
- How do I set the value of a React controlled component with jQuery in Puppeteer?
- How to set the DefaultRoute to another Route in React Router
- How to test a className with the Jest and React testing library
- How to fetch the new data in response to React Router change with Redux?
- How to set up Babel 6 stage 0 with React and Webpack
- How to mock history.push with the new React Router Hooks using Jest
- How to override the width of a TextField component with react MUI?
- How to upgrade a React project built with create-react-app to the next create-react-app version?
- How to detect the device on React SSR App with Next.js?
- How do I use the Firebase onAuthStateChange with the new React Hooks?
- How to set the height of button in React Native Android
- How should the new context api work with React Native navigator?
- How to set srcObject on audio element with React
- How do I use local state along with redux store state in the same react component?
- How to set the width of a React component during test?
- How to correctly set initial state in React with Typescript without constructor?
- How should customRequest be set in the Ant Design Upload component to work with an XMLHttpRequest?
- How to implement componentDidMount with hooks in React to be in line with the EsLint rule "react-hooks/exhaustive-deps": "warn"?
- How do you update Formik initial values with the react context api values after an AJAX request?
- How to TEST async calls made in componentDidMount that set the state of React Component
- How to resolve "Uncaught TypeError: Failed to construct 'Comment': Please use the 'new' operat....." with respect to React JS?
- How can I change the color of a Link in React with Material-UI?
- How do you set the Typescript type for useRef hook in React Native?
More Query from same tag
- Dynamic font-size calculate based on container size
- How to do basic validation on React Boostrap 5 input
- Save the localStorage data in array of objects in state
- Typescript type error while destructuring on empty object with default values?
- how do it pass props to each component when having this kind of structure of routing
- Want to remove / ( -) . from phone number strings
- Is there a way to externally reset a react component to its original state
- Work with multiple checkboxes in the edit form in React and react-bootstrap
- How can I set State based on other state in functional component?
- How can I conditionally map multiple arrays with if statements after fetching an array of objects? (React)
- How to get scrollPosition using getSnapshotBeforeUpdate() in React Js?
- How to pass redux store in Provider when using ReactDom.render() to the rendering element?
- I get a warning message saying "Do not mutate state directly."
- Syncing multiple unrelated components in react
- ReactJS - TypeError: this.state.user.map is not a function
- React upload file to mongodb node Js multer
- How to apply transitions to full page in material-ui and keep AppBar fixed?
- How to render element on click of button: ReactJS
- Dynamically access image in react
- React Nested Map and Filter with conditional render does not work
- useState with `new` keyword as parameter
- React cloneNode - make a copy
- Call a static function into the class React ES6
- React JS: Expected a string but got: object
- How to build a advanced filter component in react?
- React Addup number During LongPress buttom?
- Unable to access token using react hooks
- Javascript react Uncaught SyntaxError: Unexpected token '<' or Syntax error
- Not able to access react state from socket event handler function
- How to get data from an async function in React