iamdustan / smoothscroll

Scroll Behavior polyfill
http://iamdustan.com/smoothscroll/
MIT License
3.85k stars 340 forks source link

Adding instruction for use with React projects. #139

Open JOLee83 opened 5 years ago

JOLee83 commented 5 years ago

In the Index.js file.

import * as smoothscroll from 'smoothscroll-polyfill';

smoothscroll.polyfill();

Thanks for this, works great and it was exactly what I needed for a project.

Dakkers commented 5 years ago

it's already in the readme

fredspivock commented 3 years ago

Having trouble getting it to work with React + Typescript. I have it in my index.tsx but it does not seem to work when I run my app in the iOS simulator.

florian-wilisch commented 3 years ago

This doesn't work for me... :(

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import * as smoothscroll from 'smoothscroll-polyfill';

smoothscroll.polyfill();

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

This is in the index.js file. Am I doing anything wrong?!

Dakkers commented 3 years ago

@florian-wilisch try importing it before React, e.g.

// Polyfills
import * as smoothscroll from 'smoothscroll-polyfill';
// The rest :)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

smoothscroll.polyfill();

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
florian-wilisch commented 3 years ago

@Dakkers , unfortunately that didn't work. :-/

galvaodev commented 3 years ago

@fredspivock Solution for Typescript, add in an index.tsx hook for useEffect. It needs to load first as soon as the page is rendered.

Example:

import * as smoothscroll from 'smoothscroll-polyfill' useEffect(() => { smoothscroll.polyfill() }, [])