Reactjs react-router redux syncHistoryWithStore崩溃/通过状态与react-router
基本上,问题是我无法使用react-redux和react-router传递状态。在我的组件中,我有如下代码:Reactjs react-router redux syncHistoryWithStore崩溃/通过状态与react-router,reactjs,redux,react-router,react-redux,react-router-redux,Reactjs,Redux,React Router,React Redux,React Router Redux,基本上,问题是我无法使用react-redux和react-router传递状态。在我的组件中,我有如下代码: export default connect( (state, ownProps) => ({ reduxState: state, ownProps }), dispatch => ({ onSyncState: (state) => { dispatch({ty
export default connect(
(state, ownProps) => ({
reduxState: state,
ownProps
}),
dispatch => ({
onSyncState: (state) => {
dispatch({type: 'SYNCHRONIZE_STORE', payload: state});
}
})
)(Home);
但是当我尝试控制台记录道具时,我发现我有reduxState:undefined。调度工作正常。
所以现在我尝试使用react router redux来同步它们
在我的index.js文件中,我有如下代码:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import Home from './Home/Home.component';
import Edit from './Edit/Edit.component';
import './index.css';
import { Router, Route, browserHistory, Redirect, IndexRoute } from 'react-router';
import { createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { Provider } from 'react-redux';
import { syncHistoryWithStore } from 'react-router-redux';
import categoriesService from './Services/Categories.service'
const initialState = {
tasks: categoriesService.getTasks(),
categories: categoriesService.getCategories(),
currentCategory: 1,
filterString: '',
showDone: 'show'
};
function reducer(state = initialState, action) {
if (action.type === 'SYNCHRONIZE_STORE') {
console.log(action.payload);
console.log(state);
}
}
const store = createStore(reducer, composeWithDevTools());
const history = syncHistoryWithStore(browserHistory, store);
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Redirect from="/" to="/home/1/show/" />
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="/home/:category/:showDone(/:filter)" component={Home} />
<Route path="/edit/:category/:task" component={Edit} />
</Route>
<Redirect from="*" to="/home/1/show/" />
</Router>
</Provider>,
document.getElementById('root')
);
从“React”导入React;
从“react dom”导入react dom;
从“./App”导入应用程序;
从“./Home/Home.component”导入Home;
从“./Edit/Edit.component”导入编辑;
导入“./index.css”;
从“react Router”导入{Router,Route,browserHistory,Redirect,IndexRoute};
从“redux”导入{createStore};
从'redux devtools extension'导入{composeWithDevTools};
从'react redux'导入{Provider};
从“react router redux”导入{syncHistoryWithStore};
从“./Services/Categories.service”导入分类服务
常量初始状态={
任务:categoriesService.getTasks(),
类别:categories Service.getCategories(),
当前类别:1,
筛选器字符串:“”,
showDone:“show”
};
函数缩减器(状态=初始状态,动作){
如果(action.type==='SYNCHRONIZE\u STORE'){
console.log(action.payload);
console.log(状态);
}
}
const store=createStore(reducer,composeWithDevTools());
const history=syncHistoryWithStore(浏览器历史记录,存储);
ReactDOM.render(
,
document.getElementById('root'))
);
categoriesService是一个模拟的同步服务,它只返回模拟的数据数组。
当我尝试使用syncHistoryWithStore执行此操作时,我得到一个错误:
Uncaught TypeError: Cannot read property 'routing' of undefined
at defaultSelectLocationState (sync.js:14)
at syncHistoryWithStore (sync.js:37)
at eval (index.js:62)
at Object.<anonymous> (bundle.js:1372)
at __webpack_require__ (bundle.js:556)
at fn (bundle.js:87)
at eval (multi_main:3)
at Object.<anonymous> (bundle.js:589)
at __webpack_require__ (bundle.js:556)
at bundle.js:579
Uncaught TypeError:无法读取未定义的属性“routing”
在默认的SelectLocationState(sync.js:14)
在syncHistoryWithStore(sync.js:37)
评估时(索引:62)
反对。(bundle.js:1372)
at\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuwpack\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
在fn(bundle.js:87)
评估时(多回路主管道:3)
反对。(bundle.js:589)
at\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuwpack\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
在bundle.js:579
所以,我没有主意了。希望您能帮助修复react router redux,或以其他方式传递状态。:) 试着在
控制台.log()中使用:…state
。对不起,伙计们,这只是我的愚蠢。:)我忘了在我的工作结束时返回状态。:)而且,我忘了将reducer与routing结合起来,以便与syncHistoryWithStore一起使用。看起来我真的不知道该怎么做
下面是一个工作示例:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import Home from './Home/Home.component';
import Edit from './Edit/Edit.component';
import './index.css';
import { Router, Route, browserHistory, Redirect, IndexRoute } from 'react-router';
import { createStore, combineReducers } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { Provider } from 'react-redux';
import { syncHistoryWithStore, routerReducer } from 'react-router-redux';
import categoriesService from './Services/Categories.service'
const initialState = {
tasks: categoriesService.getTasks(),
categories: categoriesService.getCategories(),
currentCategory: 1,
filterString: '',
showDone: 'show'
};
function reducer(state = initialState, action) {
if (action.type === 'SYNCHRONIZE_STORE') {
console.log(action);
console.log(...state);
}
return state;
}
let combinedReducers = combineReducers({
routing: routerReducer,
reducer
});
const store = createStore(combinedReducers, composeWithDevTools());
const history = syncHistoryWithStore(browserHistory, store);
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Redirect from="/" to="/home/1/show/" />
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="/home/:category/:showDone(/:filter)" component={Home} />
<Route path="/edit/:category/:task" component={Edit} />
</Route>
<Redirect from="*" to="/home/1/show/" />
</Router>
</Provider>,
document.getElementById('root')
);
从“React”导入React;
从“react dom”导入react dom;
从“./App”导入应用程序;
从“./Home/Home.component”导入Home;
从“./Edit/Edit.component”导入编辑;
导入“./index.css”;
从“react Router”导入{Router,Route,browserHistory,Redirect,IndexRoute};
从'redux'导入{createStore,combinereducer};
从'redux devtools extension'导入{composeWithDevTools};
从'react redux'导入{Provider};
从“react router redux”导入{syncHistoryWithStore,routerReducer};
从“./Services/Categories.service”导入分类服务
常量初始状态={
任务:categoriesService.getTasks(),
类别:categories Service.getCategories(),
当前类别:1,
筛选器字符串:“”,
showDone:“show”
};
函数缩减器(状态=初始状态,动作){
如果(action.type==='SYNCHRONIZE\u STORE'){
控制台日志(操作);
console.log(…状态);
}
返回状态;
}
设combinedReducers=组合减速机({
路由:routerReducer,
减速器
});
const store=createStore(组合的导出器,composeWithDevTools());
const history=syncHistoryWithStore(浏览器历史记录,存储);
ReactDOM.render(
,
document.getElementById('root'))
);
请提供完整的代码。哦,很抱歉,我忘了在我的答案中写这句话,无论如何,快乐编码:)@Codesingh无需担心:非常感谢。欢迎兄弟:)