redux tutorial: const store in this.props



我正在做react教程,视频#24

https://egghead.io/lessons/javascript-redux-passing-the-store-down-explicitly-via-props

组件映射:

TodoApp -> VisibleTodoList -> FilterLink

我只需要知道为什么这段代码在VisibleTodoList和FilterLink组件:"const {store} = this。Props "这是。Props中的第一个元素吗?要在底部查看控制台日志。

class VisibleTodoList extends Component {
  componentDidMount() {
    const { store } = this.props;
    this.unsubscribe = store.subscribe(() =>
      this.forceUpdate()
    );
  }
  componentWillUnmount() {
    this.unsubscribe();
  }
  render() {
    const props = this.props;
    const { store } = props;
    const state = store.getState();
    return (
      <TodoList
        todos={
          getVisibleTodos(
            state.todos,
            state.visibilityFilter
          )
        }
        onTodoClick={id =>
          store.dispatch({
            type: 'TOGGLE_TODO',
            id
          })
        }
      />
    );
  }
}
class FilterLink extends Component {
  componentDidMount() {
    const { store } = this.props;
    this.unsubscribe = store.subscribe(() =>
      this.forceUpdate()
    );
  }
  .
  . // Rest of component as before down to `render()`
  .
  render() {
    const props = this.props;
    const { store } = props
    const state = store.getState()
    .
    . // Rest of component as before
    .
  }
}
const TodoApp = ({ store }) => (
  <div>
    <AddTodo store={store} />
    <VisibleTodoList store={store} />
    <Footer store={store} />
  </div>
);
const render = () => {
  ReactDOM.render(
    <TodoApp store={createStore(todoApp)} />,
    document.getElementById('root')
  );
};
store.subscribe(render);

FilterLink

当我打印这个。我有两个元素:store和proto,这很明显。

Object {store: Object}
store : Object
    dispatch :
    dispatch(action) getState: getState()
    replaceReducer : replaceReducer(nextReducer)
    subscribe : subscribe(listener)
    Symbol(observable) : observable()
    __proto__ : Object
__proto__ : Object

但是当我打印这个。在控制台上我有FilterLink组件的道具:(我不明白这个顺序,存储对象是第一个元素吗?)

Object {filter: "SHOW_ALL", store: Object, children: "All"}
    children :    "All"
    filter :  "SHOW_ALL"
store : Object
__proto__   : Object 

当我在控制台'store'上打印FilterLink组件时,我得到:

Object {}
dispatch    :    dispatch(action)
getState  :   getState()
replaceReducer   :     replaceReducer(nextReducer)
subscribe   :    subscribe(listener)
Symbol(observable)   : observable()
__proto__   :
Object

我需要了解更多关于"const {store} = this"的信息。Props ",我不太清楚

const { store } = this.props正在使用ES6对象解构。

将一个常量赋值给右边的对象,取出与变量同名的键的值(在本例中为store),并将其赋值给存储变量。它相当于

const store = this.props.store

相关内容

  • 没有找到相关文章

最新更新