首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactJS为同一组件调用多个实例

ReactJS为同一组件调用多个实例
EN

Stack Overflow用户
提问于 2018-12-05 15:08:56
回答 1查看 949关注 0票数 0

我试图从我的App.js文件中调用组件两次,但在渲染时,我得到了该组件的相同实例。此组件将从Unsplash获得随机图像。现在我已经想出了一些解决方案,比如先清空实例,然后再次调用组件。如何通过再次调用同一组件来创建该组件的新实例?

Code: //Parallex.js

代码语言:javascript
复制
import React from 'react';
import $ from 'jquery';
import { Button } from 'reactstrap';
import './components/style/style.css';


class Parallex extends React.Component {
    constructor() {
        super();
        this.para = this.para.bind(this);
    }
    componentDidMount = () => {
        this.para();
        document.addEventListener('scroll', this.isInViewport, true);
    }


    para() {
        $(document).ready(function(){

        var scrolled = $(window).scrollTop()
        console.log("--------->" + scrolled);
        $('.parallax').each(function(index) {
            var imageSrc = $(this).data('image-src')
            var imageHeight = $(this).data('height')
            $(this).css('background-image','url(' + imageSrc + ')')
            $(this).css('height', imageHeight)
            var initY = $(this).offset().top
            var height = $(this).height()
            var diff = scrolled - initY
            var ratio = Math.round((diff / height) * 100)
            $(this).css('background-position','center ' + parseInt(-(ratio * 1.5)) + 'px')
        })

        $(window).scroll(function() {
          var scrolled = $(window).scrollTop()
          $('.parallax').each(function(index, element) {
            var initY = $(this).offset().top
            var height = $(this).height()
              var diff = scrolled - initY
              var ratio = Math.round((diff / height) * 100)
              $(this).css('background-position','center ' + parseInt(-(ratio * 1.5)) + 'px')
          })
        })
      })
    }

    render() {
        return (
            <div ref='UniqueElementIdentifies' className="parallax" id="parallax-2" data-image-src="**https://source.unsplash.com/random/1920x1081"** data-height="400px">
                <div className="caption">
                    <Button>Know more</Button>
                </div>
            </div>
        )
    }
}

export default Parallex;

App.js:

代码语言:javascript
复制
import React, { Component } from 'react';
import NavbarComponent from './components/js/Navbar';
import Example from './carousal';
import Parallex from './parallex';
import FooteraboutUs from './footer-aboutus';
import {
  Jumbotron,
  Button,
 } from 'reactstrap';
import './App.css';
import './Sticky-Navbar.css';

class App extends Component {

  render() {
    return (
      <div>
      <div>
          <Parallex/>
        </div>
      <div>
        <Example/>
        </div>
        <div>
          <Parallex/>
        </div>
        <div>
        <FooteraboutUs/>
      </div>
      </div>
    );
  }
}
export default App;
EN

回答 1

Stack Overflow用户

发布于 2018-12-05 15:21:51

因此,当我们在循环中渲染组件时,我们给每个组件一个键。你能试着给你的组件一个密钥吗?键应该给组件的多个副本提供不同的身份。

例如:<Parallex key={1} />

KEYS

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53626955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档