반응 구성 요소 (즉, 별도의 도우미 함수) 외부에 저장소 인스턴스 (저장소 상태)를 가져 오려고합니다. 내 감속가, 나의 행동, 나는 가장 위의 구성 요소에 상점을 만들었습니다. React Redux - 헬퍼 함수에서 기존 저장소에 액세스
configStore.js
import { createStore } from 'redux';
import generalReducers from '../reducers/generalReducers';
export default function configStore(initialState) {
return createStore(generalReducers, initialState);
}
index.js
import { Provider } from 'react-redux';
import configStore from './store/configStore';
const initialReduxStoreConfig = {
unit: 'm2',
language: 'en'
}
const store = configStore(initialReduxStoreConfig);
ReactDOM.render((
<Provider store={store}>
<App/>
</Provider>
), document.querySelector('#root'));
helpers.js
import configStore from '../store/configStore';
const store = configStore();
function getTranslation(key, lang = null) {
console.log(store.getState());
}
도움 주셔서 감사합니다.
스토어 초기화 코드를 별도의 모듈로 옮기고, 스토어를 내보내고, 필요할 때마다 가져올 수 있습니다. – Flying
좀 더 구체적으로 알려주시겠습니까? 별도의 파일에 저장소를 만들고 있는데, index.js의 configStore() 함수를 호출하고 있습니다. – DavidN
아래 예제에서 해결책을 찾으십시오. – Flying