1

I'm writing a simple cursor record webpage with Reactjs, the problem is the records array only updates whenever I click the button while my intention for it is to continuously update on mouse move. The logic actually works fine on raw javascript, there's must be something about how react events work that i'm mistaken. The code:

import React, {Component} from 'react';

class MouseRecord extends Component {
    constructor(props) {
        super(props);

        this.state = {
            records: [],
            displayMessage: false
        }

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

    handleClick() {
        console.log(this.state.records);
        this.setState(prevState => ({
            //records: [],
            displayMessage: !prevState.displayMessage
        }));
    }

    handleMouseMove(e) {
        e.preventDefault();
        if (this.state.records.length <= 100000){
            this.setState(prevState => ({
                records: prevState.records.concat([{
                    cor_X: e.clientX,
                    cor_Y: e.clientY,
                    width: window.innerWidth,
                    height: window.innerHeight,
                    timestamp: new Date().toUTCString()
                }])
            }));
        }
    }

    componentDidMount() {
        document.addEventListener('mousemove', (e) => this.handleMouseMove(e));
    }

    componentWillUnmount() {
        document.removeEventListener('mousemove', (e) => this.handleMouseMove(e));
    }

    render(){
        return(
            <div className="box">
                <button onClick={this.handleClick}>Click to retrieve mouse records</button>
                <div>
                    {this.state.displayMessage ? this.state.records.length > 0 ? this.state.records.map((record) => {
                        return (
                            <div key={this.state.records.indexOf(record)}>
                                <p>client X: {record.cor_X}</p>
                                <p>client Y: {record.cor_Y}</p>
                                <p>Window width: {record.width}</p>
                                <p>Window height: {record.height}</p>
                                <p>Timestamp: {record.timestamp}</p>
                                <br/>
                                <br/>
                            </div>
                        );
                    }) : null : null}
                </div>
            </div>
        );
    }
}

export default MouseRecord;
  • Your general logic is correct, the mouse position is being saved on every mouse move event. But you're only _displaying_ the mouse position list once `this.state.displayMessage` is set to true after that first mouse click. – Sly_cardinal Jul 16 '21 at 06:31
  • It might be worth posting this on https://codereview.stackexchange.com/ since there are some other issues with the code that are outside the scope of stackoverflow (e.g. incorrect mousemove event listener removal and unnecessary `records.indexOf()` inside the `records.map()`) – Sly_cardinal Jul 16 '21 at 06:43
  • 1
    @Sly_cardinal, you should mention that it will not be [acceptable on Code Review](//codereview.stackexchange.com/help/dont-ask) until it's working correctly. – Toby Speight Jul 16 '21 at 07:51
  • @Sly_cardinal It was my intention to toggle the mouse position list like that. What I'm trying to figure out is why the list only updates when I click on the button, or in other words when `this.state.displayMessage` is set to true. I want the list to continuously update when I move the cursor around, so expecting like 50+ object or sth each time I display the list, sadly it doesn't work like that. – Benedict Howard Jul 16 '21 at 10:09
  • @Sly_cardinal To be more specific the first time I click on the button, the list only has 1 mouse position (like i said before I'm expecting 50+ or sth) and when I click again the list length only ascend by 1, which is again really weird. About all the other problems you mentioned, I'll try to fix it but the core logic is my first priority right now, i'm still a newbie in all of this. – Benedict Howard Jul 16 '21 at 10:13

0 Answers0