redux教程:在this.props中的const存储

sta*_*ave 4 redux react-redux

我正在做教程反应,视频#24

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

组件地图:

TodoApp - > VisibleTodoList - > FilterLink

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

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);
Run Code Online (Sandbox Code Playgroud)

FilterLink

当我在控制台上打印this.props for VisibleTodoList Component时,我有两个元素: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
Run Code Online (Sandbox Code Playgroud)

但是当我在控制台上打印this.props for FilterLink Component时,我有:(我不明白这个顺序,存储objet它是第一个元素吗?)

Object {filter: "SHOW_ALL", store: Object, children: "All"}
    children :    "All"
    filter :  "SHOW_ALL"
store : Object
__proto__   : Object 
Run Code Online (Sandbox Code Playgroud)

当我在FilterLink组件的控制台"商店"上打印时,我得到:

Object {}
dispatch    :    dispatch(action)
getState  :   getState()
replaceReducer   :     replaceReducer(nextReducer)
subscribe   :    subscribe(listener)
Symbol(observable)   : observable()
__proto__   :
Object
Run Code Online (Sandbox Code Playgroud)

我需要更多地了解"const {store} = this.props",这对我来说并不清楚.

小智 6

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

为右侧的对象分配一个常量,拉出与变量同名的键的值(在这种情况下store,并将其赋值给store变量.它相当于

const store = this.props.store
Run Code Online (Sandbox Code Playgroud)