Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question about coordinating two components #10

Open
jvsteiner opened this issue Dec 7, 2024 · 3 comments
Open

question about coordinating two components #10

jvsteiner opened this issue Dec 7, 2024 · 3 comments

Comments

@jvsteiner
Copy link

jvsteiner commented Dec 7, 2024

In my use case, I would like to have an interactive Chessboard in one part of the component tree, and a MoveText in another part. I would like them to share the same Game state: ie. the user might interact with either of them, and I want the other one to be synchronized. Is this something that this library is designed for/capable of? If so, would it be possible to offer a few hints about how to do it?

@jvsteiner
Copy link
Author

jvsteiner commented Dec 8, 2024

I ended up using redux based on the POJO's you provided. I had to reimplement game navigation login in the reducer, and also add support for adding new variations, etc. - good learning exercise. I appreciate your decision to make the POJO representation available, that was a very farsighted design choice.

@yo35
Copy link
Owner

yo35 commented Dec 10, 2024

Hi,
Sorry for the late reply.

If you just need to synchronize a NavigationBoard and Movetext, it should not be that complex. A minimal example could be like:

function NavBoardWithMoveText({ game }) {

  const [nodeId, setNodeId] = useState('start');
  
  function onSelectedInMovetext(n) {
      if (n !== undefined) {
          setNodeId(n);
      }
  }

  return (
    <>
        <NavigationBoard game={game} nodeId={nodeId} onNodeIdChanged={setNodeId} />
        <Movetext game={game} selection={nodeId} interactionMode="selectMove"
            onMoveSelected={onSelectedInMovetext} />
    </>
  );

}

Of course, in a real-life code, you may want to lift the nodeId higher in the component tree, possibly persisting it with redux or any other persistency mechanism, but the key points regarding component synchronization are here.

@jvsteiner
Copy link
Author

Yes, you are right - I have about three consumers of the state stretched from one end of the tree to the other, so I went with redux, since I had been meaning to learn how to use it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants