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

React 18 example #66

Open
maxjowett opened this issue Jan 9, 2024 · 1 comment
Open

React 18 example #66

maxjowett opened this issue Jan 9, 2024 · 1 comment

Comments

@maxjowett
Copy link

Is anyone able to run this library in React 18? If so could you provide an example?

@Spojowa
Copy link

Spojowa commented Feb 18, 2024

You can try something like this:

import React, {useEffect, useRef, useState} from 'react'
import './App.css'
import {Fretboard, Position} from "@moonwave99/fretboard.js";

function App() {
    const fretboardRef = useRef<Fretboard>()
    const fretboardDomRef = useRef(null)

    const [lastPosition, setLastPosition] = useState<Position>({
        string: 1,
        fret: 1
    })

    useEffect(() => {
        if (fretboardRef.current == null) {
            fretboardRef.current = new Fretboard({
                el: fretboardDomRef.current,
                fretColor: 'blue',
                dotFill: 'red',
                dotText: (dot: Position) => dot.fret
            })
                .render()
                .on('mousemove', (position) => setLastPosition(position))
        } else {
            const fretboard = fretboardRef.current
            fretboard.clear()
                .setDots([lastPosition])
                .render()
        }
    })

    return (
        <>
            <figure ref={fretboardDomRef} style={{width: '800px'}}></figure>
        </>
    )
}

export default App

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