Giter VIP home page Giter VIP logo

Comments (3)

KamranAsif avatar KamranAsif commented on July 23, 2024

Best way to do this is to inform your application in componentDidUpdate that it has finished scrolling to the top, so the subsequent render doesn't set scrollToRow to zero.

Here is a very simple example: https://jsfiddle.net/z2yhpwLh/
Feel free to create your own jsfiddle and we can help you debug it.

from fixed-data-table-2.

amit1911 avatar amit1911 commented on July 23, 2024

hey thanks for the prompt response.
I am using React with Redux so i wanted to somehow keep the scrollToTop in the state and wanted to have a smooth redux flow. componentDidUpdate is what I was looking for. Here is how i did it

class CustomTable extends React.Component {
    componentWillReceiveProps(nextProps) {
        const { tableSettings: newTableSettings } = nextProps;
        const { tableSettings: oldTableSettings } = this.props;

        this.scrollToRow = null;

        // scroll table to top only if scrollTop changed from false -> true
        if (!oldTableSettings.scrollToTop && newTableSettings.scrollToTop) {
            this.scrollToRow = 0;
        }
    }

    componentDidUpdate() {
        // reset scroll state to false if it was true. this is so that we can
        // detect the next scrollToTop event
        if (this.props.tableSettings.scrollToTop) {
            this.props.resetScrollState();
        }
    }

    render() {
        return (
            <Table
                scrollToRow={this.scrollToRow}
            >
            </Table>
        );
    }
}

I have removed all the extra data from the above code so that it's easy to understand.

So basically the main logic is that I will only scroll the table to top when the value of scrollToTop changes from false to true. And after the scrollToTop is changed to true i will change it back to false during the componentDidUpdate lifecycle by dispatching RESET_SCROLL_STATE action which will change the scrollToTop state back to false. Now it will scroll to top only when the event happens.
Let me know what you think about it.

from fixed-data-table-2.

KamranAsif avatar KamranAsif commented on July 23, 2024

Looks great! Let me know if you see any bugs with that in the future.

from fixed-data-table-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.