背景それぞれがコレクションから削除できる要素のコレクションのコンテナを作成しようとしています。要素が削除されると、その出口をアニメートしたいと思い、React Motionを使用してこれを達成しようとしています。ここでリアクションモーションで要素の終了をアニメーション化する(親要素からこれらの要素を受け取る)
は、上記の図です:
問題私は、モーションのTransitionMotion
コンポーネントを反応させるの使用を考え、それに渡される必要がある関数を記述しようとして捕まってしまいました。
class Container extends Component {
state = {
elementStyles: {}
}
componentDidMount() {
this.getElementStyles();
}
componentDidUpdate() {
this.getElementStyles();
}
getElementStyles() {
if (!this.props.children.length || this.hasElementStyles()) return;
// this assumes all elements passed to the container are of equal dimensions
let firstChild = this.refs.scroller.firstChild;
let elementStyles = {
width: firstChild.offsetWidth,
height: firstChild.offsetHeight,
opacity: 1
};
this.setState({
elementStyles
});
}
hasElementStyles() {
return !isEmpty(this.state.elementStyles); // lodash to the rescue
}
willLeave() {
return { width: spring(0), height: spring(0), opacity: spring(0) }
}
getChildrenArray() {
return Children.toArray(this.props.children); // that's React's util function
}
getModifiedChild(element, props) {
if (!element) return;
return React.cloneElement(
element,
{style: props.style}
);
}
getInitialTransitionStyles() {
let elements = this.getChildrenArray();
let result = elements.map((element, index) => ({
key: element.key,
style: this.state.elementStyles
}));
return result;
}
render() {
if (this.hasElementStyles()) {
return (
<TransitionMotion
willLeave={this.willLeave}
styles={this.getInitialTransitionStyles()}
>
{ interpolatedStyles => {
let children = this.getChildrenArray();
return <div ref="scroller" className="container">
{ interpolatedStyles.map((style, index) => {
return this.getModifiedChild(children[index], style);
})
}
</div>
}}
</TransitionMotion>
);
} else {
return (
<div ref="scroller" className="container">
{ this.props.children }
</div>
);
}
}
}
お知らせTransitionMotionコンポーネント内のマップ関数内の行::return this.getModifiedChild(children[index], style)
- 間違った - ここに私のコードです。要素がコレクションから削除されると、this.props.children
が変更されますので、間違っている、とchildren
配列のインデックスは、もはや子どもたちから算出しstyles
配列のインデックスに対応していないでしょう。
私が必要とするのは、要素がコレクションから削除される前と後でprops.children
を追跡するための巧妙な方法です(そして、私は考えることができません;私が考え出すことができる最高のものは、find
配列return this.getModifiedChild(children.find(child => child.key === style.key), style);
の機能が、それは)それについて考えるために、私は怖いループ内で非常に多くのループになります、またはいくつかの完全に異なるアプローチを使用するために、私は知りません。助けてもらえますか?