geelen / react-snapshot

A zero-configuration static pre-renderer for React apps
MIT License
1.66k stars 104 forks source link

Snapshot of async re-render (setState) #122

Open pedroBruno7 opened 5 years ago

pedroBruno7 commented 5 years ago

Hi

When my React code has a fetch (inside componentDidMount for example) or other async operation (like setTimeout) that has a setState inside the callback, the snapshot does not detect that new html render, even when the snapshotDelay is very big.

A simple component example:

class Users extends Component {

    state = {
        data: null
    }

    componentDidMount() {

        setTimeout(() => {

            this.setState(
                {
                    data: [
                       2, 3, 4, 2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4,2, 3, 4
                    ]
                })
        }, 1000);

    }

   render() {

        return (
            <div>
                <p>Users page !!!</p>
                {this.state.data}
            </div>
        )
   }

}

The snapshot html file will not have that data on it, even tough I have a delay set up with:

"reactSnapshot": { "snapshotDelay": 5000 }

Being the setTimeout 1000 ms, why it does not appear on snapshot? Is it not just a temporal snap of the current page html? I confirmed and the each snap waits 5 seconds on the console

I assume this is related to the issues found here: https://github.com/geelen/react-snapshot/issues/32 https://github.com/geelen/react-snapshot/issues/68

@geelen Can you help? Big thanks

arnespremberg commented 4 years ago

@pedroBruno7 did you find a solution for this? I'm running into the same problem

johnlin0207 commented 4 years ago

me too,how to fix it?

pedroBruno7 commented 3 years ago

@arnespremberg @johnlin0207 Hi, I did not find any viable solution with the react snapshot libraries out there My advice is: use gatsby for this kind of stuff, it's a very robust project with extensive documentation. You can balance static and dynamic react code and even trigger new "snapshots" when data changes on your API