gpt4 book ai didi

redux 教程 : const store in this. Prop

转载 作者:行者123 更新时间:2023-12-05 00:17:38 25 4
gpt4 key购买 nike

我正在做教程 react ,视频 #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 的控制台日志并为每个组件存储对象。

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);

过滤器链接

当我在控制台上为 VisibleTodoList 组件打印 this.props 时,我有两个元素: 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 组件打印 this.props 时,我有:
(我不明白这个顺序,store objet 它是第一个元素?)
Object {filter: "SHOW_ALL", store: Object, children: "All"}
children : "All"
filter : "SHOW_ALL"
store : Object
__proto__ : Object

当我在控制台“存储”上为 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 object destructuring .

为右侧的对象分配一个常量 like 提取与变量同名的键的值(在本例中为 store 并将其分配给 store 变量。它相当于

const store = this.props.store

关于redux 教程 : const store in this. Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39668122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com