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

本文共 5224 字,大约阅读时间需要 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'));

     

    转载于:https://www.cnblogs.com/Answer1215/p/5184879.html

    你可能感兴趣的文章
    windows上面链接使用linux上面的docker daemon
    查看>>
    Redis事务
    查看>>
    Web框架和Django基础
    查看>>
    python中的逻辑操作符
    查看>>
    CSS兼容性常见问题总结
    查看>>
    HDU 1548 A strange lift (Dijkstra)
    查看>>
    每天一个小程序—0005题(批量处理图片大小)
    查看>>
    C# 启动进程和杀死进程
    查看>>
    tcp实现交互
    查看>>
    IIS的各种身份验证详细测试
    查看>>
    JavaScript特效源码(3、菜单特效)
    查看>>
    聊聊、Zookeeper Linux 单服务
    查看>>
    Linux常用命令总结
    查看>>
    yii模型ar中备忘
    查看>>
    C#线程入门
    查看>>
    CSS清除浮动方法
    查看>>
    JVM内存回收机制简述
    查看>>
    洛咕 P2480 [SDOI2010]古代猪文
    查看>>
    js-创建对象的几种方式
    查看>>
    JDK JRE Java虚拟机的关系
    查看>>