博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Redux] Passing the Store Down with <Provider> from React Redux
阅读量:6828 次
发布时间:2019-06-26

本文共 5165 字,大约阅读时间需要 17 分钟。

Previously, we wrote the Provider component by ourself:

class Provider extends Component {  getChildContext() {    return {      store: this.props.store    };   }  render() {    return this.props.children;  }}Provider.childContextTypes = {  store: React.PropTypes.object};

 

Then we can get 'store' from the 'context' from each child container component:

class VisibleTodoList extends Component {  componentDidMount() {    const { store } = this.context;    this.unsubscribe = store.subscribe(() =>      this.forceUpdate()    );  }    componentWillUnmount() {    this.unsubscribe();  }    render() {    const props = this.props;    const { store } = this.context;    const state = store.getState();        return (      
store.dispatch({ type: 'TOGGLE_TODO', id }) } /> ); }}VisibleTodoList.contextTypes = { store: React.PropTypes.object};

 

or Persentiaonal component:

const AddTodo = (props, { store }) => {  let input;  return (    
{ input = node; }} />
);};AddTodo.contextTypes = { store: React.PropTypes.object};

 

Because the solution is so convenient, we can use library instead of writing our own Provider:

So we can remvoe all the whole Provider class component, instead of we do:

const {Provider} = ReactRedux;

In html:

 

---------

Code:

  
JS Bin
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) {        return state;      }      return {        ...state,        completed: !state.completed      };    default:      return state;  }};const todos = (state = [], action) => {  switch (action.type) {    case 'ADD_TODO':      return [        ...state,        todo(undefined, action)      ];    case 'TOGGLE_TODO':      return state.map(t =>        todo(t, action)      );    default:      return state;  }};const visibilityFilter = (  state = 'SHOW_ALL',  action) => {  switch (action.type) {    case 'SET_VISIBILITY_FILTER':      return action.filter;    default:      return state;  }};const { combineReducers } = Redux;const todoApp = combineReducers({  todos,  visibilityFilter});const { Component } = React;const Link = ({  active,  children,  onClick}) => {  if (active) {    return {children};  }  return (     {         e.preventDefault();         onClick();       }}    >      {children}      );};class FilterLink extends Component {  componentDidMount() {    const { store } = this.context;    this.unsubscribe = store.subscribe(() =>      this.forceUpdate()    );  }    componentWillUnmount() {    this.unsubscribe();  }    render() {    const props = this.props;    const { store } = this.context;    const state = store.getState();        return (      
store.dispatch({ type: 'SET_VISIBILITY_FILTER', filter: props.filter }) } > {props.children} ); }}FilterLink.contextTypes = { store: React.PropTypes.object};const Footer = () => (

Show: {

' '}
All
{', '}
Active
{', '}
Completed

);const Todo = ({ onClick, completed, text}) => (
  • {text}
  • );const TodoList = ({ todos, onTodoClick}) => (
      {todos.map(todo =>
      onTodoClick(todo.id)} /> )}
    );let nextTodoId = 0;const AddTodo = (props, { store }) => { let input; return (
    { input = node; }} />
    );};AddTodo.contextTypes = { store: React.PropTypes.object};const getVisibleTodos = ( todos, filter) => { switch (filter) { case 'SHOW_ALL': return todos; case 'SHOW_COMPLETED': return todos.filter( t => t.completed ); case 'SHOW_ACTIVE': return todos.filter( t => !t.completed ); }}class VisibleTodoList extends Component { componentDidMount() { const { store } = this.context; this.unsubscribe = store.subscribe(() => this.forceUpdate() ); } componentWillUnmount() { this.unsubscribe(); } render() { const props = this.props; const { store } = this.context; const state = store.getState(); return (
    store.dispatch({ type: 'TOGGLE_TODO', id }) } /> ); }}VisibleTodoList.contextTypes = { store: React.PropTypes.object};const TodoApp = () => (
    );const {Provider} = ReactRedux;const { createStore } = Redux;ReactDOM.render(
    , document.getElementById('root'));

     

    转载地址:http://qmukl.baihongyu.com/

    你可能感兴趣的文章
    linux 下查找文件或者内容常用命令
    查看>>
    Linux常用系统调用表
    查看>>
    linux x86_64要注意的问题
    查看>>
    批处理中的call与start的个人学习心得
    查看>>
    搜索引擎的前世今生
    查看>>
    JSP
    查看>>
    经典排序算法 - 地精排序Gnome Sort
    查看>>
    Java中main函数参数String args[] 和 String[] args 区别
    查看>>
    puppet FAQ
    查看>>
    Struts2.0+Hibernate2.5+Spring3.0搭建JavaEE项目要用的jar
    查看>>
    互联网
    查看>>
    MySQL load data 权限相关
    查看>>
    ScriptManager.RegisterStartupScript失效的解决方案
    查看>>
    vsftpd 添加用户
    查看>>
    第三方模块的安装
    查看>>
    Terracotta中锁与性能的问题
    查看>>
    遇到Linux系统安装时窗口过大,按钮点不到,该怎么解决
    查看>>
    js 判断输入是否为正整数
    查看>>
    「收藏」一些有趣的图
    查看>>
    探索虚函数(二)
    查看>>