サブスクリプションの部分では正しい軌道に乗っていますが、Reduxはすばらしく、他のFluxのような状態管理パターンと同様に、子コンポーネントを更新できるように、子チェーンのプロパティを渡す必要はありません以下のようなので、(あなたがしたかったが、できなかった場合に必要):
function Parent() {
return <ChildOne color="red" />
}
function ChildOne(props) {
return <ChildTwo color={props.color} />
}
function ChildTwo(props) {
return <h1>The Color was: {props.color}</h1>
}
それはかもしれないコンポーネントオブジェクト方にプロパティを更新する"dispatch"
(Reduxの/フラックス用語)状態ストアにaction
にあなたを可能にに加入する。
「接続」のライブラリはreact-reduxです。それは多くの機能を持っていますが、私が見ている主なのは、あなたが購読したいと思っているreduxストアの部分を含むより多くのロジックでコンポーネントを "ラップする"高次コンポーネント(HOC)であるconnect
です。
ので、上記のようになります。
最大の違いは、それが内
childTwo
対象に「サブスクライブ」だったので、あなたは
ChildTwo
に
Parent
2ダウンのレベルから色を渡すために持っていなかったということです
export class Parent extends React.Component {
componentDidMount() {
this.props.dispatch(changeColor('red'));
}
render() {
return <ChildOne />
}
}
export default connect((state) => ({ //This property is the redux store
parent: state.parent,
}))(Parent) //higher order component that wraps the component with redux functionality
function ChildOne(){
return (
<ChildTwo />
);
}
export function ChildTwo(props) { //will have childTwo bound in props object
return (
<h1>The Color is: {props.childTwo.color}
);
}
export default connect((state) => ({ //This property is the redux store
childTwo: state.childTwo,
}))
reduxストアとそのビットの状態をコンポーネントに接続したので、ストアの変更によってコンポーネントが状態変更から再レンダリングされます。あなたが唯一の深い1つの層を下っていると、コンテナコンポーネントは、AJAXのようなロジックを処理しているような特性の通過は理にかなって
プロパティの通過とReduxのは、このPresentation
のmedium postとContainer
コンポーネントとより多くの意味を行います使用して、リクエスト、またはディーラーの一部への発送など。
このスレッドは役に立ちました: https://github.com/reactjs/redux/issues/1287 gaearonはreduxの作成者です。彼はまたこの記事を書いた: https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367#.5lexgsyv6 – Conan