Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - 我无法使用Redux Hooks更新Redux的状态(I can't update the state from Redux with Redux Hooks)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...