A
A
Alfieros2019-05-06 17:35:30
JavaScript
Alfieros, 2019-05-06 17:35:30

How to initialize a script on render in React?

I connect the " Peppermint " library to the react project in index.html, it does not work via import in the component, the library is in the nodes. The problem is to initialize the script inside the component on render. That is, now everything works during the initial rendering of the DOM. If rendering of other components (switching between pages, etc.) stops working, you need to reload the DOM.

import React from 'react';
import c from './Category.module.css';
import Peppermint from '@seangenabe/peppermint';
 // При вызове из компоненты пишет: 'Peppermint' is defined but never used  no-unused-vars, но не знаю как сделать инициализацию.



const Category = (props) =>{
return(
    <div className={c.category}>

        <div className="peppermint peppermint-inactive" id="peppermint">
            <div className={c.swipe__page}><h2 className={c.title__page}>Filter</h2></div>
            <div className={c.swipe__page}><h2 className={c.title__page}>Category</h2></div>
            <div className={c.swipe__page}><h2 className={c.title__page}>Another Page</h2></div>
        </div>
    </div>

    );
   }
   export default Category;

If you cannot call it as a module, is it possible to somehow call this function inside the component? Initialization via JS
var slider = Peppermint(document.getElementById('peppermint'));

I ran out of ideas, I couldn't find the answer in Google. Help with a solution please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-05-06
@mrsexy

Use lifecycle methods:
componentDidMount for initialization,
componentDidUpdate for reinitialization,
shouldComponentUpdate to prevent unnecessary redraws.

A
Alexey Nikolaev, 2019-05-06
@Heian

Obviously, it is initialized with reference to specific DOM elements. The solution is simpler than a steamed turnip, re-initialize with each render.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question