Reactjs url会更改,但组件不会更改
我想从saga中间件重定向,但我的做法只是更改了url,但显示了我当前所在的页面。这就是我所做的Reactjs url会更改,但组件不会更改,reactjs,redux,react-router-dom,redux-saga,redux-toolkit,Reactjs,Redux,React Router Dom,Redux Saga,Redux Toolkit,我想从saga中间件重定向,但我的做法只是更改了url,但显示了我当前所在的页面。这就是我所做的 function forwardTo(location) { console.log("location", history, location); history.push(location); } yield call(forwardTo, "/"); 我正在使用redux工具包,这是我的设置 export function configure
function forwardTo(location) {
console.log("location", history, location);
history.push(location);
}
yield call(forwardTo, "/");
我正在使用redux工具包,这是我的设置
export function configureAppStore() {
const reduxSagaMonitorOptions = {};
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
const { run: runSaga } = sagaMiddleware;
// // Create the store with saga middleware
const middlewares = [sagaMiddleware];
const enhancers = [
createInjectorsEnhancer({
createReducer,
runSaga,
}),
];
const store = configureStore({
reducer: createReducer(),
middleware: [...getDefaultMiddleware(), ...middlewares],
devTools:
/* istanbul ignore next line */
process.env.NODE_ENV !== "production" ||
process.env.PUBLIC_URL.length > 0,
enhancers,
});
// Make reducers hot reloadable, see http://mxs.is/googmo
/* istanbul ignore next */
if (module.hot) {
module.hot.accept("./reducers", () => {
forceReducerReload(store);
});
}
return store;
}
app.js
import { BrowserRouter as Router } from "react-router-dom";
<Provider store={store}>
<Router>
<RouterApp />
</Router>
</Provider>
utils/history.js
import { createBrowserHistory } from "history";
const history = createBrowserHistory();
export default history;
如何从saga中间件重定向到特定路由
我不使用连接路由器的原因是
我不太确定,所以我想了解从saga中发送路由器操作的推荐方法
更新代码
export function createReducer(injectedReducers = {}) {
// Initially we don't have any injectedReducers, so returning identity function to avoid the error
const rootReducer = combineReducers({
...injectedReducers,
router: connectRouter(history),
});
return rootReducer;
}
index.js
const ConnectedApp = ({ Component }) => (
<Provider store={store}>
<ConnectedRouter history={history}>
<Component />
</ConnectedRouter>
</Provider>
);
const render = (Component) => {
ReactDOM.render(<ConnectedApp Component={Component} />, MOUNT_NODE);
};
const ConnectedApp=({Component})=>(
);
常量渲染=(组件)=>{
render(,MOUNT_节点);
};
我发现以下问题未捕获在状态树中找不到路由器reducer,它必须安装在“router”下我们用于向redux应用商店发送导航操作。我是否可以直接在我的reducer.js上导入历史并创建
createRootReducer(历史)
?否。如果您阅读了使用步骤,您将创建一个函数,该函数接受一个历史对象(createRootReducer(history)
),并创建一个历史对象以传递给连接的react路由器
中间件。您是如何在saga中导入历史的?它是通过从“历史”导入{createBrowserHistory};这可能需要在几个地方,所以我有这个在utils。
const ConnectedApp = ({ Component }) => (
<Provider store={store}>
<ConnectedRouter history={history}>
<Component />
</ConnectedRouter>
</Provider>
);
const render = (Component) => {
ReactDOM.render(<ConnectedApp Component={Component} />, MOUNT_NODE);
};