使用CSSTransitionGroup和Redux Connect

时间:2017-09-19 11:51:27

标签: reactjs react-redux reactcsstransitiongroup

我试图在组件中添加一些动画样式,并努力找出出错的地方。这是一个带有connect()和CSSTransitionGroup的简单组件(DOM中其他地方的不同组件最终将用于触发打开我的灯箱组件,Redux将用于在它们之间共享状态,以防你想知道为什么它'要求):

LightboxPresentation.js:

import React, { Component } from 'react';
import { CSSTransitionGroup } from 'react-transition-group';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as actionCreators from '../actions/actionCreators';

class LightboxPresentation extends Component {
    render() {
        return (
            <div>
            <CSSTransitionGroup
                transitionName="lightbox"
                transitionEnterTimeout={500}
                transitionLeaveTimeout={500}>
                {this.renderPage()}
                </CSSTransitionGroup>
                </div>
        )
    }
    renderPage() {
        return (
            <div key={'lightbox-module'}>Some test HTML</div>
        );
    }
}

const mapStateToProps = (state) => {
    return {
        showLightbox: state.showLightbox,
        itemsShowLightbox: state.itemsShowLightbox,
    };
};
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators(actionCreators, dispatch);
};
export default connect(mapStateToProps, mapDispatchToProps)(LightboxPresentation);

这是调用上述组件的代码:

App.js:

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from './store/configureStore';

import ItemList from './components/ItemList';
import LightboxPresentation from './components/LightboxPresentation';

const initialState = { itemFilter: 'featured' }

const store = configureStore(initialState);

render(
    <Provider store={store}>
        <LightboxPresentation />
    </Provider>,
    document.getElementById('lightbox')
);

但是我的控制台出现以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of `LightboxPresentation`.
    in LightboxPresentation
    in Connect(LightboxPresentation)
    in Provider

如果我从我的renderPage调用周围删除该块,则renderPage的输出呈现而没有错误。它只会添加转换块导致错误。据我所知,import {CSSTransitionGroup}是正确的,它是如何在documentatio n中完成的(虽然我确实没有花括号但仍然没有运气)。

我做错了吗?我花了一些时间尝试不同的事情和谷歌搜索,但到目前为止一切都没有喜悦。

1 个答案:

答案 0 :(得分:2)

您使用的是反应过渡组的版本2+吗?版本2之后没有CSSTransitionGroup。如果您希望代码按照原样运行,请尝试通过npm install --save react-transition-group@1.2.0进行还原。

如果您想使用新版本,请尝试使用These Docs中的新API。