gpt4 book ai didi

javascript - 未找到错误 : Failed to execute 'removeChild' on 'Node' on React with Flickity

转载 作者:行者123 更新时间:2023-12-04 11:40:52 27 4
gpt4 key购买 nike

我正在构建一个 react 应用程序,在我的主页上,我有组件“CategoriesList”。

当我在主页上时,“类别列表”运行良好,但是当我使用 react-router-dom 导航到 ProductDetails 页面时,我发现了这个错误:

"NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node."



'CategoriesList' 使用 Flickty。我试图删除 Flickity,并且......效果很好。但我需要使用 Flickity。

谁能帮我?

类别列表组件:
const CategoryList = ({list, popupOpen, refreshProductList}) => {

return (
<Container>
<Slider
options={{
cellAlign: 'center',
draggable: true,
groupCells: true,
contain: false,
pageDots: false,
}}

style={ popupOpen ? ({opacity: 0.05}) : null}
>
{list.map((category, index) => (
<Category key={index}>{category}</Category>
))}

</Slider>
</Container>
);
}

Flickty slider 组件:
import Flickity from 'flickity';
import 'flickity/dist/flickity.min.css';

export default class Slider extends Component {
constructor(props) {
super(props);

this.state = {
flickityReady: false,
};

this.refreshFlickity = this.refreshFlickity.bind(this);
}

componentDidMount() {
this.flickity = new Flickity(this.flickityNode, this.props.options || {});

this.setState({
flickityReady: true,
});
}

refreshFlickity() {
this.flickity.reloadCells();
this.flickity.resize();
this.flickity.updateDraggable();
}

componentWillUnmount() {
this.flickity.destroy();
}

componentDidUpdate(prevProps, prevState) {
const flickityDidBecomeActive = !prevState.flickityReady && this.state.flickityReady;
const childrenDidChange = prevProps.children.length !== this.props.children.length;

if (flickityDidBecomeActive || childrenDidChange) {
this.refreshFlickity();
}
}

renderPortal() {
if (!this.flickityNode) {
return null;
}

const mountNode = this.flickityNode.querySelector('.flickity-slider');

if (mountNode) {
return ReactDOM.createPortal(this.props.children, mountNode);
}
}

render() {
return [
<div style={this.props.style} className={'test'} key="flickityBase" ref={node => (this.flickityNode = node)} />,
this.renderPortal(),
].filter(Boolean);
}
}

最佳答案

如果你想将 Flickity 与 React 一起使用,而不是创建自己的组件,我强烈建议使用 react-flickity-component .它也很容易使用:

Tip: if you use wrapAround option in Flickity setdisableImagesLoaded prop ture (default is false).

import Flickity from 'react-flickity-component'

const flickityOptions = {
autoPlay: 4000,
wrapAround: true
}

function Carousel() {
return (
<Flickity
disableImagesLoaded
options={flickityOptions} // Takes flickity options {}
>
<img src="/images/placeholder.png"/>
<img src="/images/placeholder.png"/>
<img src="/images/placeholder.png"/>
</Flickity>
)
}

关于javascript - 未找到错误 : Failed to execute 'removeChild' on 'Node' on React with Flickity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037992/

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