使用Redux Toolkit简化store的创建。
javascriptimport { configureStore } from '@reduxjs/toolkit';import counterReducer from './features/counterSlice';export const store = configureStore({ reducer: { counter: counterReducer, },});
4 React组件与Redux Store交互
4.1 使用Provider包裹应用
为了让React组件可以访问Redux store,需要在顶层组件使用
<Provider>组件。
jsximport React from 'react';import ReactDOM from 'react-dom';import { Provider } from 'react-redux';import store from './store';import App from './App';ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root'));