supasate / connected-react-router

A Redux binding for React Router v4
MIT License
4.73k stars 593 forks source link

Upgrade to RRv6? #397

Open Brodzko opened 4 years ago

Brodzko commented 4 years ago

react-router is about to release v6 soon (as of today, v6.0.0-alpha.2 is out). Will connected-react-router be compatible with the new version, or is an upgrade planned soon?

Sorry if I missed any info on this.

supasate commented 4 years ago

I just started working on it on the dev branch. Now, it passes all unit tests, but I haven't tested it on some sample apps and will do during weekends. Feel free to check it out and let me know if it works.

Note that there is a minor issue on RRv6 on history listening that causes a memory leak warning. I tried to fix it in this PR https://github.com/ReactTraining/react-router/pull/7195.

lukasluecke commented 4 years ago

@supasate Could you maybe release a preview version that's working with v6? I'd like to test it in my project and give feedback 🙂

stephenkao commented 4 years ago

Just bumping this issue and echoing the request for a preview version release (if possible)! react-router v6 entered beta stage about a month ago, and I'm looking forward to upgrading to and using the latest versions of these packages.

nikakoy131 commented 4 years ago

Hello. I created a temporary solution for react-router 6 navigation from redux-saga (a very simple version of connected-react-router). It includes redux middleware, reducer, one action (push), and BrowserRouter component. You can try it unless the new version of connected-react-router will have been created. https://gist.github.com/nikakoy131/09cbbaa2563871ff2d2955c7681a3727 It very poor tested, and have @reduxjs/toolkit and typescript as dependency Usage: Configure store

// in store.ts
import { createBrowserHistory } from 'history';
import { configureStore } from '@reduxjs/toolkit';
import { routerReducer, routerMiddleware } from './customReduxRouter';
const sagaMiddleWare = createSagaMiddleware();
export const history = createBrowserHistory({ window });
const store = configureStore({
  reducer: {
    router: routerReducer,
  },
  middleware: [sagaMiddleWare, routerMiddleware(history)],
});

sagaMiddleWare.run(rootSaga);

export type AppState = ReturnType<typeof store.getState>;

export default store;

In your index.tsx

import App from './App';
import store, { history } from './redux/store';
import { BrowserRouter } from './redux/customReduxRouter';
ReactDOM.render(
  <Provider store={store}>
    {/* <ConnectedRouter history={history}> */}
    <BrowserRouter history={history}>
      <App />
    </BrowserRouter>
    {/* </ConnectedRouter> */}
  </Provider>,
  document.getElementById('root'),
);

In your saga

import { routerActions } from '../customReduxRouter';
import { put } from 'redux-saga/effects';

function* someSuperSaga() {
 // your logic ...
 yield put(routerActions.push('/'));
}
phistuck commented 4 years ago

See https://github.com/supasate/connected-react-router/pull/438 for a temporary fix. I am not sure it is the correct/performant way to support the new React Router v6-beta.0, but it works.

elnazabo commented 3 years ago

Hello @supasate, I was wondering about the status on this issue. Would be awesome to get some updates! Also thanks for the great work! 👍

chandlervdw commented 3 years ago

Ping ping. @supasate any chance for an alpha or beta version with support for react-router@v6^?

oscar-gallog commented 3 years ago

Same here, I'm getting tons of errors trying to implement this on react-router-dom v6.0.0-beta, like this one: Error: useLocation() may be used only in the context of a <Router> component.

oscar-gallog commented 3 years ago

I installed the version on the dev branch, and now I just got this error: Module not found: Can't resolve 'connected-react-router' in 'project/src/App.tsx'

sergeushenecz commented 2 years ago

react router was released to 6.0.0 version. Do you need update compatibility with react-router?

b0yblake commented 2 years ago

Currently, connected-react-router just supports React router v4/5. I tested it, it ran with error @.@

lovewinders commented 2 years ago

When to support React router v6?

aviyi commented 2 years ago

Any updated?

schilffarth commented 1 year ago

When will we get this for v6?

petro-shkuratenyuk commented 1 year ago

More than 2 years, still no update?

Astarosa commented 1 year ago

I think we can consider it will never be done

gkamesh98 commented 4 months ago

I think currently they don't provide support in near future. If you still want to use it with react-router@v6. You can make small tweak as follows. Instead of using the ConnectedRouter You can make following default react-router Router component and make changes as follows

import React, { useLayoutEffect, useRef, useState } from 'react'
import { Route, Router, Routes } from 'react-router-dom'

import { createBrowserHistory } from 'history'
import { useDispatch } from 'react-redux'
import { onLocationChanged } from 'connected-react-router'

const history = createBrowserHistory()

export default ({ children }) => {
  const isFirstRender = useRef(true)
  const dispatch = useDispatch()
  const [historyState, setHistoryState] = useState({
    action: history.action,
    location: history.location,
  })

  useLayoutEffect(() => {
    return history.listen(setHistoryState)
  }, [])

  useLayoutEffect(() => {
    if (isFirstRender.current) {
      dispatch(onLocationChanged(history.location, history.action, true))
      isFirstRender.current = false
    }
    return history.listen((arg) => {
      dispatch(onLocationChanged(arg.location, arg.action, false))
    })
  }, [dispatch])

  return (
    <Router
      // ...rest of props required
      location={historyState.location}
      action={historyState.action}
      navigator={history}
    >
      {/* here it lies <Routes> <Route> ..... */}
      {children}
    </Router>
  )
}

Rest of it is same as before. I hope it is helpful. Thank you.