0

I am getting this error in my component in useState (I believe), based on this stacktrace:

    at resolveDispatcher (react.development.js:1590)
    at useState (react.development.js:1618)
    at GameTable (index.js:15)
    at Module../src/components/GameTable/index.js (index.js:83)
    at __webpack_require__ (bootstrap:785)
    at fn (bootstrap:150)
    at Module../src/components/Home/index.js (index.js:83)
    at __webpack_require__ (bootstrap:785)
    at fn (bootstrap:150)
    at Module../src/components/App/index.js (index.js:74)
    at __webpack_require__ (bootstrap:785)
    at fn (bootstrap:150)
    at Module../src/index.js (index.css?e32c:37)
    at __webpack_require__ (bootstrap:785)
    at fn (bootstrap:150)
    at Object.1 (serviceWorker.js:137)
    at __webpack_require__ (bootstrap:785)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
    at main.chunk.js:1

I don't understand why this error is happening though, since I am using the hooks inside of a function, as it says to. I have checked that all of my dependencies are up to date, so there shouldn't be any version conflicts, like React suggests, and I don't have two versions of React in the same project. So I assume I'm breaking some Rule of Hooks, but I don't know what it is I'm doing wrong.

My component is here:

import React, { useState, useEffect } from "react";
import { withFirebase } from "../Firebase";
import TableContainer from "@material-ui/core/TableContainer";
import makeStyles from "@material-ui/core/styles/makeStyles";
import withAuthorization from "../Session/withAuthorization";
import Paper from "@material-ui/core/Paper";
import withStyles from "@material-ui/core/styles/withStyles";
import Table from "@material-ui/core/Table";
import PropTypes from 'prop-types';
import TableRow from "@material-ui/core/TableRow";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableBody from "@material-ui/core/TableBody";

function GameTable(props) {
    const [loading, setLoading] = useState(false);
    const [games, setGames] = useState([]);

    const useStyles = makeStyles({
        table: {
            minWidth: 640,
        }
    });

    useEffect(() => {
       setLoading(true);

        props.firebase.games(props.firebase.auth().currentUser.uid).on('value', snapshot => {
            const gamesObject = snapshot.val();

            const gamesList = [];
            Object.keys(gamesObject).forEach((key) => {
                gamesList.push({[key]: gamesObject[key]})
            });

            setGames(gamesList);
            setLoading(false);
        });

        return function cleanup() {
            props.firebase.games().off();
        }
    });

    const classes = useStyles();

    return (
        <TableContainer component={Paper}>
            <Table className={classes.table}>
                <TableHead>
                    <TableRow>
                        <TableCell>Date</TableCell>
                        <TableCell>Opponent</TableCell>
                        <TableCell>Goals</TableCell>
                        <TableCell>Assists</TableCell>
                        <TableCell>Points</TableCell>
                    </TableRow>
                </TableHead>
                <TableBody>
                    {games.map(game => (
                        <TableRow key={game.date}>
                            <TableCell component="th" scope="row">
                                {game.date}
                            </TableCell>
                            <TableCell align="right">{game.opponent}</TableCell>
                            <TableCell align="right">{game.goals}</TableCell>
                            <TableCell align="right">{game.assists}</TableCell>
                            <TableCell align="right">{game.points}</TableCell>
                        </TableRow>
                    ))}
                </TableBody>
            </Table>
        </TableContainer>
    );
}

GameTable.propTypes = {
        classes: PropTypes.object.isRequired,
};

export default withAuthorization(withFirebase(GameTable()));
Cameron
  • 1,281
  • 1
  • 19
  • 40
  • Functional components are instanceless, there is no `this`. Is that the full error? Any more of a stack trace you can provide? Also, your export should probably not invoke `GameTable`, i.e. `GameTable` instead of `GameTable()`. Doing so calls it *like a function* versus later as a component. – Drew Reese Jan 12 '20 at 19:40
  • I have updated the stack trace to be the full thing. I am exporting it as GameTable() because doing so fixes an error detailed here: https://stackoverflow.com/questions/59705437/ . I'm not sure that that's the correct thing to do, but it fixes that error, and instead causes this one – Cameron Jan 12 '20 at 19:48
  • 1
    use `setLoadging` instead `this.setState({loading...`, `setGames` instead ... – xadm Jan 12 '20 at 19:59
  • You're right, I should already be doing that. I tried that and updated the code above. I also removed all reference to `this`, but got the same error – Cameron Jan 12 '20 at 20:04
  • What is at line 83 in `GameTable/index.js`? Is it that export statement? – Drew Reese Jan 12 '20 at 20:29
  • Yeah that's the last line, the export – Cameron Jan 12 '20 at 20:32
  • And if you remove the parens (i.e. immediate invocation) do you still have a hooks error? – Drew Reese Jan 12 '20 at 20:33
  • Nope, then I get the error I referenced above in that other question. Functions are not valid as a React child – Cameron Jan 12 '20 at 20:36
  • move `useStyles` definition outside of component – xadm Jan 12 '20 at 22:18
  • No change. The error is with useState, not useStyles – Cameron Jan 12 '20 at 22:21
  • I bet on firebase related things, check https://stackoverflow.com/a/56213515/6124657 – xadm Jan 12 '20 at 23:19
  • I don't think it is. That question you linked to isn't particularly relevant, and the issue is coming from useState, not Firebase. I tried a couple of things from the question you linked, and none of them fixed anything – Cameron Jan 12 '20 at 23:59
  • make code a little cleaner - like https://stackoverflow.com/a/58710950/6124657 - explicit listener, cleaner - listener detached from [unmounted] component can result with such error – xadm Jan 13 '20 at 01:47

0 Answers0