【问题标题】:Correct way to use CSSTransitionGroup and Redux Connect正确使用 CSSTransitionGroup 和 Redux Connect 的方法
【发布时间】:2017-09-30 15:09:07
【问题描述】:

我正在尝试向组件添加一些动画样式,并努力找出问题出在哪里。这是一个带有 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 } 是正确的,这就是他们在documentation 中的做法(尽管我确实尝试过不带花括号但仍然没有运气)。

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

【问题讨论】:

    标签: reactjs react-redux reactcsstransitiongroup


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 2019-10-20
      • 2019-04-10
      • 1970-01-01
      • 2018-06-06
      • 2016-01-08
      • 2019-11-22
      • 1970-01-01
      相关资源
      最近更新 更多