我是Redux钩子和React钩子的新手,这是我的代码不会停止渲染吗?
const App: React.FC = () => {
const dispatch = useDispatch();
const [page, setPage] = useState(1);
const fetchUsers = async (page: number) => {
dispatch({
type: FETCH_USERS_REQUEST,
payload: { page }
});
try {
const { data, ...result } = await api.fetchUsers(page);
const user = new schema.Entity('data');
const users = normalize(
{ users: data },
{
users: [user]
}
);
dispatch({
type: FETCH_USERS_SUCCESS,
payload: {
...result,
users
}
});
} catch (error) {
dispatch({ type: FETCH_USERS_FAILURE, payload: { error } });
}
};
useEffect(() => {
fetchUsers(1);
}, [fetchUsers]);
const users = useSelector((state: RootState) => state.users);
console.log('asd', users);
return (
<div className="vh-100 vw-100">
<header>Users</header>asdasd
</div>
);
};
fetchUsers是一个异步方法,我打算在loadMore和分页上多次使用,但是,这不起作用,我如何使其起作用?
答案 0 :(得分:2)
您的fetchUsers会在每次重绘时进行更改,这些重绘将用usefffect触发该获取。
尝试一下:
data %>%
mutate(time = as.POSIXct(time, format = '%a, %b %d %Y %T'))
# A tibble: 4 x 2
# time value
# <dttm> <dbl>
#1 2020-01-28 13:41:26 1
#2 2020-01-28 13:41:07 2
#3 2020-01-28 13:40:06 3
#4 2020-01-28 13:39:04 4