So I am trying to learn how to use useDispatch and useSelector, but I don't really know what I am doing wrong.
(因此,我正在尝试学习如何使用useDispatch和useSelector,但是我真的不知道我在做什么错。)
The problem is that instead of replacing my state with the new one, it deletes it.(问题在于,它没有删除新状态,而是将其删除。)
index.jsx
(index.jsx)
const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk)));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
actionCreators.js
export const fetchGitHubTrendingTopics = () => {
return dispatch => {
return fetch('https://api.github.com/search/topics?q=javascript', {
headers: {
Accept: 'application/vnd.github.mercy-preview+json'
}
})
.then(res => res.json())
.then(json => {
dispatch({
type: 'FETCH_GITHUB_TOPICS',
payload: json.items.slice(0, 5)
});
})
.catch(err => err.message);
};
};
actionTypes.js
export const FETCH_GITHUB_TOPICS = 'FETCH_GITHUB_TOPICS';
rootReducer.js
export const initialState = {
topics: [],
repos: [],
battleRepos: []
};
export function reducer(state = initialState, { type, payload }) {
switch (type) {
case FETCH_GITHUB_TOPICS:
return { ...state, topics: payload };
default:
return state;
}
}
INITIAL:
(初始:)
AFTER PRESSING THE BUTTON:(按下按钮后:)
if I do it the old way, it updates it and it works.
(如果我采用旧方法,它将对其进行更新并起作用。)
what am I doing wrong ?(我究竟做错了什么 ?)
ask by Gim-Cojocaru Raul-Cristian translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…