[Redux] Colocating selectors with Reducers

Source: Internet
Author: User

We'll learn how to encapsulate the knowledge on the state of shape in the reducer files and so that the components don ' t ha ve to rely on it.

In current visibletodolist.js:

Import {Connect} from ' React-redux '; import {withrouter} from' React-router '; import {Toggletodo} from‘.. /actions '; import ToDoList from'./todolist '; Const Getvisibletodos= (Todos, filter) = = {  Switch(filter) { Case' All ':      returnTodos;  Case' Completed ':      returnTodos.filter (t =t.completed);  Case' Active ':      returnTodos.filter (t =!)t.completed); default:      Throw NewError (' Unknown filter: ${filter}. '); }};const Mapstatetoprops= (state, {params}) = =({todos:getvisibletodos (state.todos, Params.filter|| ' All ',}); const visibletodolist=withrouter (Connect (mapstatetoprops, {Ontodoclick:toggletodo}) (todolist)); ExportdefaultVisibletodolist;

Currently, the Getvisibletodos (State.todos), depends on state ' s structure.

Move Getvisibletodos to reducer file:

Const TODO = (state, action) = = {  Switch(action.type) { Case' Add_todo ':      return{id:action.id, text:action.text, completed:false,      };  Case' Toggle_todo ':      if(State.id!==action.id) {returnState ; }      return{... state, completed:!state.completed,}; default:      returnState ; }};const Todos= (state =[{ID:0, Text:"OK", Completed:false}], action)= {  Switch(action.type) { Case' Add_todo ':      return[... state, Todo (undefined, action),];  Case' Toggle_todo ':      returnState.map (t =Todo (T, action)); default:      returnState ; }};exportdefaulttodos;export const Getvisibletodos = (state, filter) = = {  Switch(filter) { Case' All ':      returnState ;  Case' Completed ':      returnState.filter (t =t.completed);  Case' Active ':      returnState.filter (t =!)t.completed); default:      Throw NewError (' Unknown filter: ${filter}. '); }};

Then on the rootreducer, we manage the state:

Import {combinereducers} from ' redux '* as Fromtodos from './todos '= combinereducers ({ c4/>defaultgetvisibletodos= (state, filter) =    fromtodos.getvisibletodos (State.todos, filter);

Use it in Visibletodolist.js:

Import {Connect} from ' React-redux '; import {Toggletodo} from‘.. /actions '; import ToDoList from'./todolist '; import {withrouter} from' React-router '; Import {Getvisibletodos}  from'. /reducers '; Const Mapstatetoprops= (state, {params}) = = {    return{todos: getvisibletodos(State, Params.filter|| ' All '),//if filter is " then change to ' all '    };}; Const Visibletodolist=withrouter (Connect (mapstatetoprops, {Ontodoclick:toggletodo}) (todolist)); ExportdefaultVisibletodolist;

[Redux] Colocating selectors with Reducers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.