4

I wanna add button, by clicking on which page will be scrolled back to the top with animation. I know how to realize that using JQuery. I tried this code:

import React, { Component } from 'react';

import $ from 'jquery';

import goUpNarrrowImage from '../static/img/goUpNarrow.png';

class GoTopButton extends Component {
  constructor() {
    super();

    this.onclick = this.onclick.bind(this);
  }

  render() {

    return ( 
      <button>
        <img src={goUpNarrrowImage} style={{
          display: "inline-block",
          marginRight: "10px"
        }} />
        наверх
      </button>
    );
  }

  onclick() {
    $("html, body").animate({ scrollTop: 0 }, 1000);
  }
}

export default GoTopButton;

But when I click the button nothing happens.

2 Answers 2

2

you can do it easily with react component like https://www.npmjs.com/package/react-scroll-up

<ScrollToTop showUnder={0} duration={1000} >
    <button>
        <img src={goUpNarrrowImage} style={{
          display: "inline-block",
          marginRight: "10px"
        }} />
    </button>
</ScrollToTop>
Sign up to request clarification or add additional context in comments.

Comments

1

onclick is missing in button. So pass onclick to the button

<button onclick={this.onclick}>

3 Comments

HELL BRUH! Thanks a lot!
We all make small mistakes very often. Cheers :)
While this answers the question and solves OP's issue, it's worth mentioning that mixing jQuery and react should almost never be used together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.