gpt4 book ai didi

reactjs - 类型错误 : Cannot read property 'baseVal' of undefined

转载 作者:行者123 更新时间:2023-12-03 14:11:52 33 4
gpt4 key购买 nike

我正在使用react-test-renderer的渲染器对我的react组件进行单元测试。它工作正常,但使用 ReactCssTransitionGroup 后,我收到以下错误“TypeError:无法读取未定义的属性'baseVal'”。不知道如何解决这个问题

我确认这个问题是由ReactCssTransitionGroup引起的

下面是我的组件文件:

import React from 'react';
import PropTypes from 'prop-types';
import {List, ListItem, Spinner} from '@stores/reactCommon';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';

import BagListItem from './bag-list-item';

const BagList = ({bagList, loading}) => {
const items = bagList.map((item, key) => (
<ListItem key={key}>
<BagListItem
upc={item.upc}
price={item.sellingPrice}
description={item.description}
/>
</ListItem>
));
return (
<div className="bag-list">
{loading ? (
<span className="bag-list__spinner">
<Spinner size="extra-large" />
</span>
) : null}
<div className="bag-list__story-default-spacer">
<List maxHeight="70vh">
<ReactCSSTransitionGroup
transitionName="bagList"
transitionAppear={true}
transitionAppearTimeout={500}
transitionEnterTimeout={500}
transitionLeaveTimeout={300}
>
{items}
</ReactCSSTransitionGroup>
</List>
</div>
</div>
);
};

BagList.propTypes = {
bagList: PropTypes.array.isRequired,
loading: PropTypes.bool
};

export default BagList;

下面是我的单元测试文件

import React from 'react';
import renderer from 'react-test-renderer';
import BagList from '../../../../src/client/js/components/bag-list/bag-list';

describe('<BagList />', function() {
this.items = [
{
upc: '123222123',
sellingPrice: '12',
description: 'this is a description'
},
{
upc: '555123',
price: '23',
description: 'this is a description'
}
];
this.getComponent = items => {
return <BagList bagList={items} loading={false} />;
};

it('Should render <BagList /> with bagList', () => {
// this line is generating the error
const items = renderer.create(this.getComponent(this.items)).toJSON();
expect(items).toMatchSnapshot();
});
});

最佳答案

react-addons-css-transition-group是一个旧包,是 deprecated支持react-transition-group .

react-transition-group 的 v1是一个直接替代品,但在 v2 中被重写。

您可能想要移至 CSSTransition 从最新 react-transition-group .

<小时/>

话虽如此,错误来自于这段代码:

function hasClass(element, className) {
if (element.classList) return !!className && element.classList.contains(className);else return (" " + (element.className.baseVal || element.className) + " ").indexOf(" " + className + " ") !== -1;
}

...在 hasClass.js模块来自dom-helpers旧包包含 react-addons-css-transition-group .

react-test-renderer ...

...provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment

...所以它没有实现 DOM 提供的所有内容。

在本例中,它看起来像 react-test-renderer没有提供 classList 的实现在element上如此称呼element.className.baseVal抛出您所看到的错误。

关于reactjs - 类型错误 : Cannot read property 'baseVal' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55380937/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com