Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我能';t使用Redux钩子从Redux更新状态_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 我能';t使用Redux钩子从Redux更新状态

Javascript 我能';t使用Redux钩子从Redux更新状态,javascript,reactjs,redux,Javascript,Reactjs,Redux,所以我试图学习如何使用useDispatch和useSelector,但我真的不知道我做错了什么。问题是,它没有用新状态替换我的状态,而是将其删除 index.jsx const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk))); ReactDOM.render( <Provider store={store}> <App /> </Provider>

所以我试图学习如何使用useDispatch和useSelector,但我真的不知道我做错了什么。问题是,它没有用新状态替换我的状态,而是将其删除

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;
  }
}

const store=createStore(reducer、composeEnhancers(applyMiddleware(thunk));
ReactDOM.render(
按下按钮后:


如果我用老方法做,它会更新它,它会工作。我做错了什么?

你的减速机里有什么?我会很快更新这个问题。你能不能控制台.log或显示真正的错误!!!我用这个问题更新了帖子。你问如何正确使用
useDispatch
usescholector
,你没有添加代码如果您正在使用它们。请在分派
fetchGitHubTrendingTopics
操作时添加组件的代码。您是否已经检查了reducer中的负载是否为
未定义的