[Redux] Filtering Redux State with React Router Params

來源:互聯網
上載者:User

標籤:

We will learn how adding React Router shifts the balance of responsibilities, and how the components can use both at the same time.

 

Now when we click the filter, the url changes, but the todos list doesn‘t change. So continue with that.

 

Router only render the App component, and App component will get a props called ‘params‘ contains filter object and we pass this filter to VisibleTodoList:

// App.jsimport React from ‘react‘;import Footer from ‘./Footer‘;import AddTodo from ‘./AddTodo‘;import VisibleTodoList from ‘./VisibleTodoList‘;const App = ({params}) => (  <div>    <AddTodo />    <VisibleTodoList filter={params.filter}/>    <Footer />  </div>);export default App;

 

In VisibleTodoList.js, we change the mapStateToProps function, add a param ‘ownProps‘ which get from App.js. It contains the ‘filter‘ proporty. 

Then in ‘getVisibleTodos()‘ function, change the switch case to match router.

// VisibleTodoList.jsimport { connect } from ‘react-redux‘;import { toggleTodo } from ‘../actions‘;import TodoList from ‘./TodoList‘;const getVisibleTodos = (todos, filter) => {  switch (filter) {    case ‘all‘:      return todos;    case ‘completed‘:      return todos.filter(t => t.completed);    case ‘active‘:      return todos.filter(t => !t.completed);    default:      throw new Error(`Unknown filter: ${filter}.`);  }};const mapStateToProps = (state, ownProps) => {  return {    todos: getVisibleTodos(state.todos, ownProps.filter),  };};const mapDispatchToProps = (dispatch) => {  return {    onTodoClick: (id) => {      dispatch(toggleTodo(id));    },  };};const VisibleTodoList = connect(  mapStateToProps,  mapDispatchToProps)(TodoList);export default VisibleTodoList;

 

Last, change the devServer.js, app.get(‘/*‘), any router will return index.html.

import path from ‘path‘;import webpack from ‘webpack‘;import webpackDevMiddleware from ‘webpack-dev-middleware‘;import config from ‘./webpack.config.babel‘;import Express from ‘express‘;const app = new Express();const port = 3000;const compiler = webpack(config);app.use(webpackDevMiddleware(compiler, {  noInfo: true,  publicPath: config.output.publicPath,}));app.get(‘/*‘, (req, res) => {  res.sendFile(path.join(__dirname, ‘index.html‘));});app.listen(port, error => {  /* eslint-disable no-console */  if (error) {    console.error(error);  } else {    console.info(      ‘?? Listening on port %s. Open up http://localhost:%s/ in your browser.‘,      port,      port    );  }  /* eslint-enable no-console */});

 

[Redux] Filtering Redux State with React Router Params

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.