[英]how to set the initialState with useReducer React
hi thank you for reading this.您好,感谢您阅读本文。 I am working a github finder react app that uses useReducer and i am try to set the initialstate when onload to load some users instead of an empty array.
我正在使用 useReducer 的 github finder react 应用程序,我尝试在加载时设置初始状态以加载一些用户而不是空数组。 if i hard code the api data into the array, it will display as i wanted, but i want to make a GET to the api and pass the data into the array.
如果我将 api 数据硬编码到数组中,它将按照我的意愿显示,但我想对 api 进行 GET 并将数据传递到数组中。 I am very new to react, thank you all for the help
我很新来反应,谢谢大家的帮助
const GithubState = (props) => {
const initialState = {
users: [],
user: {},
repos: [],
loading: false,
};
//dispatcher
const [state, dispatch] = useReducer(GithubReducer, initialState);
//search Github users
const searchUsers = async (text) => {
setLoading();
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,
});
};
//Reducer
import {
SEARCH_USERS,
SET_LOADING,
CLEAR_USERS,
GET_USER,
GET_REPOS,
} from "../types";
//action contains type and payload
// export default (state, action) => {
const Reducer = (state, action) => {
switch (action.type) {
case SEARCH_USERS:
return {
...state,
users: action.payload,
loading: false
}
case GET_USER:
return {
...state,
user: action.payload,
loading: false
}
case GET_REPOS:
return {
...state,
repos: action.payload,
loading: false
}
case SET_LOADING:
return {
...state,
loading: true
}
case CLEAR_USERS:
return {
...state,
users: [],
loading: false
}
default:
return state;
}
};
export default Reducer;
You can just call the searchUsers()
function in useEffect()
to get some users and set the state.您只需拨打
searchUsers()
函数useEffect()
得到一些用户并设置状态。
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();
},[]);
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获取第一个异步数据的合理和建议的地方是 componentDidMount ,它在钩子世界中被转换为使用 Effect 和一个空数组作为它的依赖项
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.更多的增强,你可以打电话
.then
显示小吃店和.catch
上重试的情况下,由于某种原因失败和.finally
,以一组装载在这两种情况下错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.