Expose API via useImperativeHandle.#16
Conversation
|
Would be nice to have api usage example in README.md |
src/index.tsx
Outdated
| api?.set(config); | ||
| }, [api, config]); | ||
| useImperativeHandle(apiRef, () => { | ||
| return publicApi; |
There was a problem hiding this comment.
What about exposing the whole chessground api instead of creating custom object?
There was a problem hiding this comment.
Sure, we can do that. Done now.
Also re-exported Config, Api and Key from the chessground API to reduce the clutter of imports in code using this React wrapper. Does that sound sensible?
I've added the example demo-game component from this PR description to the README, and added |
| ReactDOM.render(<Chessground />, document.getElementById("root")); | ||
| ``` | ||
|
|
||
| ## Example: showing the moves of a game |
src/index.tsx
Outdated
| useEffect(() => { | ||
| if (divRef.current && !api) { | ||
| const chessgroundApi = ChessgroundApi(divRef.current, { | ||
| animation: { enabled: true, duration: 200 }, |
src/index.tsx
Outdated
| ...config, | ||
| }); | ||
| setApi(chessgroundApi); | ||
| } else if (divRef.current && api) { |
There was a problem hiding this comment.
is it needed? It looks like it can only be fired after line 34 completes and after that the config is already provided
There was a problem hiding this comment.
I gave it a quick test, and you're right, it doesn't look to be needed. Removed.
Following on from the suggestion in #14 , reimplemented my changes using a forwardRef and useImperativeHandle. Returns an instance of the Chessground API for the current board.
Sample code demonstrating it's use (added to the README too):