zhu-ting / Learning_React2018

This repo is for developers who want to learn the React library while learning the latest techniques currently emerging in the JavaScript language. This is an exciting time to be a JavaScript developer. The ecosystem is exploding with new tools, syntax, and best practices that promise to solve many of our development problems.
0 stars 0 forks source link

Chapter09 react-redux #10

Open zhu-ting opened 6 years ago

zhu-ting commented 6 years ago

Let’s say we have some cargo to move from Washington, DC, to San Francisco, CA. We could use a train, but that would require that we lay tracks through at least nine states so that our cargo can travel to California. This is like explicitly passing the store down the component tree from the root to the leaves. You have to “lay tracks” through every component that comes between the origin and the destination. If using a train is like explicitly passing the store through props, then implicitly passing the store via context is like using a jet airliner. When a jet flies from DC to San Francisco, it flies over at least nine states—no tracks required.

Similarly, we can take advantage of a React feature called context that allows us to pass variables to components without having to explicitly pass them down through the tree as properties. Any child component can access these context variables.

zhu-ting commented 6 years ago

Presentational components are components that only render UI elements.Container components are components that connect presentational components to the data.

react-redux supplies us with a component that we can use to set up our store in the context, the provider. We can wrap any React element with the provider and that element’s children will have access to the store via context.

The first argument is a function that maps state variables to properties. The second argument is a function that dispatches actions when events are raised.