为了账号安全,请及时绑定邮箱和手机立即绑定

React - 元素类型无效:预期是字符串或类/函数,但得到:对象

React - 元素类型无效:预期是字符串或类/函数,但得到:对象

收到一只叮咚 2022-01-01 20:35:48
我正在我的应用程序中实现react-items-carousel。我试图在我的应用程序中配置一个演示示例。这是演示代码import React from 'react';import range from 'lodash/range';import styled from 'styled-components';import ItemsCarousel from 'react-items-carousel;const noOfItems = 12;const noOfCards = 3;const autoPlayDelay = 2000;const chevronWidth = 40;const Wrapper = styled.div`  padding: 0 ${chevronWidth}px;  max-width: 1000px;  margin: 0 auto;`;const SlideItem = styled.div`  height: 200px;  background: #EEE;  display: flex;  align-items: center;  justify-content: center;  font-size: 20px;  font-weight: bold;`;const carouselItems = range(noOfItems).map(index => (  <SlideItem key={index}>    {index+1}  </SlideItem>));export default class App extends React.Component {  state = {    activeItemIndex: 0,  };  componentDidMount() {    this.interval = setInterval(this.tick, autoPlayDelay);  }  componentWillUnmount() {    clearInterval(this.interval);  }  tick = () => this.setState(prevState => ({    activeItemIndex: (prevState.activeItemIndex + 1) % (noOfItems-noOfCards + 1),  }));  onChange = value => this.setState({ activeItemIndex: value });  render() {    return (      <Wrapper>        <ItemsCarousel          gutter={12}          numberOfCards={noOfCards}          activeItemIndex={this.state.activeItemIndex}          requestToChangeActive={this.onChange}          rightChevron={<button>{'>'}</button>}          leftChevron={<button>{'<'}</button>}          chevronWidth={chevronWidth}          outsideChevron          children={carouselItems}        />      </Wrapper>    );  }}唯一不同的是我变了import ItemsCarousel from '../../src/ItemsCarousel';到import ItemsCarousel from 'react-items-carousel';我已经调用了组件App而不是AutoPlayCarousel目前在终端应用程序编译成功但在浏览器中我看到以下错误元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。检查 的渲染方法App。我在许多线程中看到此错误的原因可能多种多样,您能找出我的问题以及我做错了什么吗?
查看完整描述

2 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

我不得不更新到 "react-dom": "^16.3.0"


查看完整回答
反对 回复 2022-01-01
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

尝试提供carouselItemsas 子元素


<Wrapper>

  <ItemsCarousel

    gutter={12}

    numberOfCards={noOfCards}

    activeItemIndex={this.state.activeItemIndex}

    requestToChangeActive={this.onChange}

    rightChevron={<button>{'>'}</button>}

    leftChevron={<button>{'<'}</button>}

    chevronWidth={chevronWidth}

    outsideChevron>


    {carouselItems}


  </ItemsCarousel>

</Wrapper>


查看完整回答
反对 回复 2022-01-01
  • 2 回答
  • 0 关注
  • 307 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号