リアクショントランジショングループを使用してサイドバーコンポーネントを構築しています。次のように関連するコードが行く:React TransitionGroupが状態変更時に子をアンマウントしない
function FirstChild(props) {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
}
class Sidebar extends Component {
componentWillEnter(cb) { console.log('entering'); cb() }
componentWillLeave(cb) { console.log('leaving'); cb() }
render() {
// return sidebar jsx
}
}
class Nav extends Component {
...
toggleSidebar() {
const newState = !this.state.show;
this.setState({ show: newState })
}
render() {
return (
<TransitionGroup component={FirstChild}>
{ (this.state.show == true) ? <Sidebar /> : null }
</TransitionGroup>
)
}
}
問題は、私はサイドバーを切り替えるしようとすると、ライフサイクルフックのどれもがトリガされていないことをです。最初のクリックでは、サイドバーがDOMに追加され、componentDidMountは呼び出されますが、componentWillEnterは呼び出されません。もう一度クリックして非表示にすると、state.showは正しくfalseに設定されますが、サイドバーは非表示にならず、今回はライフサイクルフックがトリガーされません。
サイドバーを条件付きでレンダリングするために三項演算子を正しく実行したのか、なぜならTransitionGroupの下に1つの子をレンダリングするだけの理由があるのだろうかと思います(そのためにFirstChild method found hereを使用しました)。
docsでcomponentWillEnterが見つかりませんhttps://facebook.github.io/react/docs/react-component.html ...どこで入手できましたか? – Melounek
@Melounek https://medium.com/appifycanada/animations-with-reacttransitiongroup-4972ad7da286 –
右...私はそれをシミュレートし、私にとっては「入」と「離し」を正しく行いました。すべてのコードを提供してもよろしいですか?たとえば、connect()を使用している場合、この問題が発生する可能性があります。https://stackoverflow.com/questions/37092116/reacttransitiongroup-doesnt-works-with-react-redux-connected-component – Melounek