GithubHelp home page GithubHelp logo

Comments (8)

Manukyanq avatar Manukyanq commented on June 11, 2024 1

Yes, my guess seems right, when you call onArrowsChange it sets new state by triggering setArrows(arrowsData);. after that here in useArrows.ts we trigger onArrowsChange again it causes infinite loop because of Array.from which creates new arrows array with different reference .

I would think about correct solution, have to discuss with @Clariity what we can do

As an dirty temporary hack i will suggest you to add extra check in your function

const arrowDrow = (arrowsData: Square[][]) => {
        if (arrowsData.length === 0 && arrowsData !== arrows) {
            setArrows([]);
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        } else {
            // this should stop endless loop
            if(arrowsData.flat().join() === arrows.flat().join()) return

            setArrows(arrowsData);
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        }
    };

from react-chessboard.

LalaSimon avatar LalaSimon commented on June 11, 2024 1

Thank you very much @Manukyanq, it works now!!

from react-chessboard.

Clariity avatar Clariity commented on June 11, 2024 1

Thank you for responding whilst I was away on holiday @Manukyanq

For the line you references in useArrows.ts:

// callback when new arrows are set
  useEffect(() => {
    onArrowsChange?.(toArray(arrows));
  }, [arrows]);

I think we need to change the implementation so that this is only called when the change comes from within the board, and not from an external arrows prop change, and the name should be changed as to not cause confusion i.e. onArrowsBoardChange

this is because if it's coming externally from then the user can handle the change externally

from react-chessboard.

Manukyanq avatar Manukyanq commented on June 11, 2024

Hi!

take a look to useArrows.ts which is used for arrows state managment.
if you change customArrows inside your onArrowsChange function, it might be the cause of infinitive loop.

Sharing your code will be much helpful for understanding whats exactly is going on, and I can raise a PR with fix

from react-chessboard.

LalaSimon avatar LalaSimon commented on June 11, 2024

Thank you for fast answer, i will try my best, so onArrowsChange im creating this function arrowDrow:

const [arrows, setArrows] = useState<Square[][]>([]);
const arrowDrow = (arrowsData: Square[][]) => {
        if (arrowsData.length === 0 && arrowsData !== arrows) {
            setArrows([]);
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        } else {
            setArrows(arrowsData);
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        }
    };

My server is responding with broadcast like this:

 socket.on("draw_arrows", (data) => {
        socket.broadcast.to(data.roomID).emit("arrows_drawn", data.arrowsData);
    });

And then client received it with this function:

socket.on("arrows_drawn", (arrowsData) => {
        setArrows(arrowsData);
    });

My component is rendered like this:

 <Chessboard
  customArrows={arrows}
  onArrowsChange={arrowDrow}
  onPieceDrop={onDrop}
  position={game.fen()}
  boardWidth={650}
/>

from react-chessboard.

Manukyanq avatar Manukyanq commented on June 11, 2024

btw, I don't understand why you need to call setArrows(arrowsData) at all in arrowDrow function? Does not this code works as expected?

const arrowDrow = (arrowsData: Square[][]) => {
        if (arrowsData.length === 0 && arrowsData !== arrows) {
            setArrows([]);
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        } else {
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
        }
    };

in general all you need is call only socket.emit(...), don't call setArrows at all, Im almost sure it will work as expected

const arrowDrow = (arrowsData: Square[][]) => {
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
    };

from react-chessboard.

LalaSimon avatar LalaSimon commented on June 11, 2024

I tried it but it seems not working. First arrow is good but next ones are not showing for both users

const arrowDrow = (arrowsData: Square[][]) => {
            socket.emit("draw_arrows", {
                arrowsData,
                roomID,
            });
    };

I think i need to call setArrorws because i broadcast it on server side so its not coming back. When i tried to emit it instead of broadcasting its not working.

from react-chessboard.

Clariity avatar Clariity commented on June 11, 2024

fixed in #99

will release soon

from react-chessboard.

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.