38331

Map a route parameter to under a Redux store's key

Question:

There is a "location" key in the state object which is used by multiple components as data source. In the URL (very similar to google maps) I have a parameter called "location" which is a coordinate. My goal is to map that value (with some modification) to the state's "location" key. How to do that?

<strong>UPDATE</strong> The only way I could imagine is to create a middleware and react to route actions, extract the parameters from the URL somehow, then dispatch a new action that will be processed by a reducer. Or just use a reducer, not necessary having an extra middleware. But I guess this is not a good approach...

Answer1:

You can get location variable params from onEnter callback of your route, and then dispatch action to store.See example above:

<pre class="lang-js prettyprint-override">import React from 'react'; import ReactDOM from 'react-dom'; import { createStore } from 'redux'; import App from './App'; import { Route, Router, browserHistory } from 'react-router'; const store = createStore(rootReducer); const routes = ( <Route path="/location/:location" component={App} onEnter={handleEnter} /> ); function rootReducer(state = { location: {}, }, action) { switch (action.type) { case 'ADD_TO_LOCATION': return { ...state, location: action.location, }; default: return state; } return state; } function handleEnter(nextState) { // Map location data here. // Next, we are dispatching mapped location to store. store.dispatch({ type: 'ADD_TO_LOCATION', location: nextState.params.location, }); } ReactDOM.render(<Router routes={routes} history={browserHistory} />, document.getElementById('root'));

Answer2:

I've created container for the root route which takes router params on componentWillReceiveProps event and dispatch them to the store if window.location.href doesn't much previous state. You can see the code on <a href="https://github.com/Darmikon/router-redux-params" rel="nofollow">github repo</a>. I use redux-saga. Therefore it's better for me just to listen to my custom event: @@routerParams/URL_UPDATE and take all params from action.payload.

Recommend

  • React-Redux componentWillReceiveProps not triggered using api data
  • Hive command line Select query time taken incorrect if its not map reduce job in the background
  • Change state's array without changing the whole state (REACT / REDUX)
  • Xcode launch app error in Mac 10.9 Mavericks
  • Hadoop shuffle uses which protocol?
  • Using let variables in a lambda in Scheme
  • What are zone turns?
  • TCameraComponent and TVideoCaptureDevice do not initialize in Win32
  • How to extract a number from a string [duplicate]
  • How to use the resource module to measure the running time of a function?
  • Pythons argparse default value doesn't work
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Mocha throws unexpected token error for ES6 object spread operator
  • Fully customized Python Help Usage
  • sweetalert2 inputoptions from file in select example
  • Laravel 4 routing not working due to .htaccess file?
  • Google Places API - Find a company's CID and LRD
  • Iron Router: How do I send data to the layout?
  • JSON encode and decode on PHP
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • netsh acl setting (need alternative method - registry settings?)
  • how to set variables in a php include file?
  • jQuery: add elements until a particular height is reached
  • Switch to popup in python using selenium
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Fail:(TESTMODE) Transactions of this market type cannot be processed on this system
  • Implementing “partial void” in VB
  • Sending HTML Form Data to Spring REST Web Service
  • Reduction and collapse clauses in OMP have some confusing points
  • Converting a WriteableBitmap image ToArray in UWP
  • Recording logins for password protected directories
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • When should I choose bucket sort over other sorting algorithms?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`