简体   繁体   English

反应渲染

[英]React rendering

I want display a lottery wheel on my hobby project.我想在我的爱好项目上显示一个彩票轮。 I found this package in npm 'lottery-wheel' and tried to install it and render it without any success.我在 npm 'lottery-wheel' 中找到了这个 package 并尝试安装它并渲染它没有任何成功。

I installed it with:我安装了它:

npm install lottery-wheel

I included it in the component-file:我将它包含在组件文件中:

import Wheel from 'lottery-wheel';

I want to render it within the div (id: 'wheel'):我想在 div 中渲染它(id:'wheel'):

    render() {
          const wheel = new Wheel({
            el: document.querySelector("#wheel"),
            data: [{
              text: 'apple',
              chance: 20
            }, {
              text: 'banana'
            }, {
              text: 'orange'
            }, {
              text: 'peach'
            }],
            onSuccess(data) {
              console.log(data.text);
            }
          });
    
        return (
                <div id="wheel">
                    {wheel}
                </div>
        );
    }

I don't know where to put this when I have a structure llike this...当我有这样的结构时,我不知道把它放在哪里......

class Lottery extends Component {

}

I can simply get the correct element with document.querySelector("#wheel") but please educate me on a good practice to render components and subcomponents using this main structure (class extends Component) Thanks!我可以简单地使用 document.querySelector("#wheel") 获得正确的元素,但请教我一个使用这个主要结构(类扩展组件)渲染组件和子组件的好习惯谢谢!

It doesn't matter if it's a class or a functional component.不管是 class 还是功能组件。 In either you can and should place their sub-components in render() (after adequate import , of course).在任何一个中,您都可以并且应该将它们的子组件放在render()中(当然,在足够的import之后)。

In your particular case, your Wheel component needs to get an existing DOM element ( el attribute) to be rendered into, and your #wheel element is rendered after the Wheel initialization and doesn't exist yet during.在您的特定情况下,您的Wheel组件需要获取要呈现的现有 DOM 元素( el属性),并且您的#wheel元素在Wheel初始化之后呈现,并且在此期间还不存在。 The issue is ordering of functions (unfortunately for you, render() always comes last).问题在于函数的排序(不幸的是, render()总是排在最后)。

The simple solutions here would be: a) create a #wheel element in your index.html (not recommended since it interferes with the main React concept of rendering the whole App into one div );这里的简单解决方案是:a)在您的index.html中创建一个#wheel元素(不推荐,因为它会干扰将整个 App 呈现为一个div的主要 React 概念); b) initialize your Wheel in a componentDidMount() (if you don't know about React lifecycle, read about it - quite fundamental) or its hook equivalent - those are simply executed after render() : b) 在componentDidMount()中初始化Wheel (如果你不了解 React 生命周期,请阅读它 - 非常基础)或其等效的钩子 - 这些只是在render()之后执行:

useEffect(() => {
    //your initialization here
}, []);

Try using a ref https://reactjs.org/docs/refs-and-the-dom.html尝试使用参考https://reactjs.org/docs/refs-and-the-dom.html

Something along the lines of类似的东西

class Wheel extends React.Component {
  constructor(props) {
    super(props);
    this.wheelRef = React.createRef();
  }

  componentDidMount() {
    const wheel = new Wheel({
      el: this.wheelRef.current,
      data: [
        {
          text: 'apple',
          chance: 20,
        },
        {
          text: 'banana',
        },
        {
          text: 'orange',
        },
        {
          text: 'peach',
        },
      ],
      onSuccess(data) {
        console.log(data.text);
      },
    });
  }

  render() {
    return <div ref={this.wheelRef} />;
  }
}

export default Wheel;


// Other file 
import Wheel from '../wheel';


render () {
  return <Wheel />
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM