react-router-redux vs redux-first-history vs connected-react-router vs react-router
React ルーティングライブラリ
react-router-reduxredux-first-historyconnected-react-routerreact-router類似パッケージ:

React ルーティングライブラリ

React ルーティングライブラリは、React アプリケーション内でのナビゲーションを管理するためのツールです。これらのライブラリは、URL に基づいて異なるコンポーネントを表示し、ユーザーがアプリケーション内を移動できるようにします。これにより、シングルページアプリケーション(SPA)の体験を向上させ、状態管理とルーティングを統合することが可能になります。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
react-router-redux319,5087,772-19年前MIT
redux-first-history90,52145399.3 kB152年前MIT
connected-react-router04,700444 kB176-MIT
react-router056,2524.14 MB1561ヶ月前MIT

機能比較: react-router-redux vs redux-first-history vs connected-react-router vs react-router

状態管理との統合

  • react-router-redux:

    react-router-redux は、React Router と Redux を連携させるためのライブラリで、ルーティングの変更を Redux アクションとしてディスパッチします。これにより、アプリケーションの状態とルーティングを同時に管理できます。

  • redux-first-history:

    redux-first-history は、Redux ストアを中心にルーティングを管理するアプローチを提供します。ルーティングの履歴を Redux ストアに保存し、アプリケーションの状態とルーティングを一元管理できます。

  • connected-react-router:

    connected-react-router は、Redux ストアと密接に統合されており、URL の変更を Redux アクションとして扱います。これにより、アプリケーションの状態とルーティングが常に同期され、状態管理が一元化されます。

  • react-router:

    react-router は、状態管理の機能を持たず、ルーティングの基本的な機能に特化しています。状態管理は別途行う必要があり、Redux などのライブラリと組み合わせて使用することが一般的です。

学習曲線

  • react-router-redux:

    react-router-redux は、React Router と Redux の両方の知識が必要です。両方のライブラリに慣れている場合はスムーズに使用できますが、初心者には少し難しいかもしれません。

  • redux-first-history:

    redux-first-history は、Redux の知識が必要ですが、ルーティングの履歴管理に特化しているため、特定のユースケースにおいては学習が容易です。

  • connected-react-router:

    connected-react-router は、Redux の知識が必要なため、学習曲線がやや急です。Redux の概念に慣れていない場合、初めての使用には時間がかかるかもしれません。

  • react-router:

    react-router は、シンプルな API を持ち、比較的簡単に学ぶことができます。基本的なルーティング機能を理解するのは容易で、初心者にも適しています。

パフォーマンス

  • react-router-redux:

    react-router-redux は、Redux の状態を使用してルーティングを管理するため、パフォーマンスは Redux の使用に依存します。適切に設計されたアプリケーションでは、パフォーマンスが向上しますが、過剰な再レンダリングが発生する可能性があります。

  • redux-first-history:

    redux-first-history は、Redux ストアを使用してルーティングを管理するため、状態管理の効率に依存します。適切に設計されたストアでは、パフォーマンスが向上しますが、複雑な状態管理がパフォーマンスに影響を与える可能性があります。

  • connected-react-router:

    connected-react-router は、Redux ストアと連携しているため、状態の変更がルーティングに直接影響を与えます。これにより、状態変更時のパフォーマンスが向上しますが、ストアのサイズが大きくなるとパフォーマンスに影響を与える可能性があります。

  • react-router:

    react-router は、軽量で高速なルーティングを提供します。特に小規模なアプリケーションでは、パフォーマンスが優れていますが、大規模なアプリケーションでは状態管理との統合が必要になる場合があります。

柔軟性

  • react-router-redux:

    react-router-redux は、React Router の柔軟性を保ちながら、Redux に統合するため、柔軟性があります。React Router の機能を活かしつつ、Redux の状態管理を利用できます。

  • redux-first-history:

    redux-first-history は、Redux ストアを使用してルーティングを管理するため、柔軟性があります。特に、カスタムな履歴管理が必要な場合に適しています。

  • connected-react-router:

    connected-react-router は、Redux のフレームワークに依存しているため、柔軟性はあるものの、Redux の制約に従う必要があります。特定のユースケースに対しては強力ですが、他の状態管理ライブラリとの統合は難しい場合があります。

  • react-router:

    react-router は、非常に柔軟で、さまざまなルーティングパターンをサポートします。カスタムルートやネストされたルートを簡単に設定でき、開発者のニーズに応じて拡張可能です。

メンテナンス性

  • react-router-redux:

    react-router-redux は、React Router と Redux の統合により、メンテナンスがやや複雑になる場合がありますが、適切に設計されたアプリケーションでは、状態管理とルーティングが一元化され、メンテナンスが容易になります。

  • redux-first-history:

    redux-first-history は、Redux ストアを使用してルーティングを管理するため、メンテナンスが容易です。特に、状態管理が明確であれば、ルーティングの管理もシンプルになります。

  • connected-react-router:

    connected-react-router は、Redux の状態管理と密接に統合されているため、アプリケーションの状態とルーティングが一元管理され、メンテナンスが容易です。ただし、Redux の複雑さがメンテナンスに影響を与える可能性があります。

  • react-router:

    react-router は、シンプルで直感的な API を持ち、メンテナンスが容易です。特に小規模なアプリケーションでは、コードが簡潔で理解しやすくなります。

選び方: react-router-redux vs redux-first-history vs connected-react-router vs react-router

  • react-router-redux:

    react-router-redux は、React Router と Redux を統合したいが、connected-react-router のような完全な統合が不要な場合に選択してください。アプリケーションの状態を Redux で管理しつつ、ルーティングの変更を追跡したい場合に便利です。

  • redux-first-history:

    redux-first-history は、Redux の状態管理を優先し、ルーティングを Redux ストアで管理したい場合に適しています。特に、ルーティングの履歴を Redux ストアに保存したい場合や、カスタムな履歴管理が必要な場合に選択してください。

  • connected-react-router:

    connected-react-router は、Redux ストアと連携してルーティングを管理したい場合に最適です。Redux の状態管理を利用して、URL の変更をトリガーし、アプリケーションの状態を同期させる必要がある場合に選択してください。

  • react-router:

    react-router は、シンプルで柔軟なルーティングを提供し、特に小規模から中規模のアプリケーションに適しています。ルーティングの基本的な機能を必要とし、Redux との統合が不要な場合に適しています。

react-router-redux のREADME

react-router-redux

npm version npm downloads build status

Keep your router in sync with application state :sparkles:

Formerly known as redux-simple-router

You're a smart person. You use Redux to manage your application state. You use React Router to do routing. All is good.

But the two libraries don't coordinate. You want to do time travel with your application state, but React Router doesn't navigate between pages when you replay actions. It controls an important part of application state: the URL.

This library helps you keep that bit of state in sync with your Redux store. We keep a copy of the current location hidden in state. When you rewind your application state with a tool like Redux DevTools, that state change is propagated to React Router so it can adjust the component tree accordingly. You can jump around in state, rewinding, replaying, and resetting as much as you'd like, and this library will ensure the two stay in sync at all times.

This library is not necessary for using Redux together with React Router. You can use the two together just fine without any additional libraries. It is useful if you care about recording, persisting, and replaying user actions, using time travel. If you don't care about these features, just use Redux and React Router directly.

Installation

npm install --save react-router-redux

How It Works

This library allows you to use React Router's APIs as they are documented. And, you can use redux like you normally would, with a single app state. The library simply enhances a history instance to allow it to synchronize any changes it receives into application state.

history + store (redux) → react-router-redux → enhanced historyreact-router

Tutorial

Let's take a look at a simple example.

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

import reducers from '<project-path>/reducers'

// Add the reducer to your store on the `routing` key
const store = createStore(
  combineReducers({
    ...reducers,
    routing: routerReducer
  })
)

// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store)

ReactDOM.render(
  <Provider store={store}>
    { /* Tell the Router to use our enhanced history */ }
    <Router history={history}>
      <Route path="/" component={App}>
        <Route path="foo" component={Foo}/>
        <Route path="bar" component={Bar}/>
      </Route>
    </Router>
  </Provider>,
  document.getElementById('mount')
)

Now any time you navigate, which can come from pressing browser buttons or navigating in your application code, the enhanced history will first pass the new location through the Redux store and then on to React Router to update the component tree. If you time travel, it will also pass the new state to React Router to update the component tree again.

How do I watch for navigation events, such as for analytics?

Simply listen to the enhanced history via history.listen. This takes in a function that will receive a location any time the store updates. This includes any time travel activity performed on the store.

const history = syncHistoryWithStore(browserHistory, store)

history.listen(location => analyticsService.track(location.pathname))

For other kinds of events in your system, you can use middleware on your Redux store like normal to watch any action that is dispatched to the store.

What if I use Immutable.js or another state wrapper with my Redux store?

When using a wrapper for your store's state, such as Immutable.js, you will need to change two things from the standard setup:

  1. By default, the library expects to find the history state at state.routing. If your wrapper prevents accessing properties directly, or you want to put the routing state elsewhere, pass a selector function to access the historystate via the selectLocationState option on syncHistoryWithStore.
  2. Provide your own reducer function that will receive actions of type LOCATION_CHANGE and return the payload merged into the locationBeforeTransitions property of the routing state. For example, state.set("routing", {locationBeforeTransitions: action.payload}).

These two hooks will allow you to store the state that this library uses in whatever format or wrapper you would like.

How do I access router state in a container component?

React Router provides route information via a route component's props. This makes it easy to access them from a container component. When using react-redux to connect() your components to state, you can access the router's props from the 2nd argument of mapStateToProps:

function mapStateToProps(state, ownProps) {
  return {
    id: ownProps.params.id,
    filter: ownProps.location.query.filter
  };
}

You should not read the location state directly from the Redux store. This is because React Router operates asynchronously (to handle things such as dynamically-loaded components) and your component tree may not yet be updated in sync with your Redux state. You should rely on the props passed by React Router, as they are only updated after it has processed all asynchronous code.

What if I want to issue navigation events via Redux actions?

React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application. However, if you prefer Redux style actions, the library also provides a set of action creators and a middleware to capture them and redirect them to your history instance.

import { createStore, combineReducers, applyMiddleware } from 'redux';
import { routerMiddleware, push } from 'react-router-redux'

// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
  reducers,
  applyMiddleware(middleware)
)

// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))

Examples

Examples from the community:

Have an example to add? Send us a PR!

API

routerReducer()

You must add this reducer to your store for syncing to work.

A reducer function that stores location updates from history. If you use combineReducers, it should be nested under the routing key.

history = syncHistoryWithStore(history, store, [options])

Creates an enhanced history from the provided history. This history changes history.listen to pass all location updates through the provided store first. This ensures if the store is updated either from a navigation event or from a time travel action, such as a replay, the listeners of the enhanced history will stay in sync.

You must provide the enhanced history to your <Router> component. This ensures your routes stay in sync with your location and your store at the same time.

The options object takes in the following optional keys:

  • selectLocationState - (default state => state.routing) A selector function to obtain the history state from your store. Useful when not using the provided routerReducer to store history state. Allows you to use wrappers, such as Immutable.js.
  • adjustUrlOnReplay - (default true) When false, the URL will not be kept in sync during time travel. This is useful when using persistState from Redux DevTools and not wanting to maintain the URL state when restoring state.

push(location), replace(location), go(number), goBack(), goForward()

You must install routerMiddleware for these action creators to work.

Action creators that correspond with the [history methods of the same name] (https://github.com/mjackson/history/blob/master/README.md#navigation). For reference they are defined as follows:

  • push - Pushes a new location to history, becoming the current location.
  • replace - Replaces the current location in history.
  • go - Moves backwards or forwards a relative number of locations in history.
  • goForward - Moves forward one location. Equivalent to go(1)
  • goBack - Moves backwards one location. Equivalent to go(-1)

Both push and replace take in a location descriptor, which can be an object describing the URL or a plain string URL.

These action creators are also available in one single object as routerActions, which can be used as a convenience when using Redux's bindActionCreators().

routerMiddleware(history)

A middleware you can apply to your Redux store to capture dispatched actions created by the action creators. It will redirect those actions to the provided history instance.

LOCATION_CHANGE

An action type that you can listen for in your reducers to be notified of route updates. Fires after any changes to history.