The callbackFinished function is not defined in the winwheel library

Every time I attempt to call a function with the callbackFinished property inside the animation of winwheel, I encounter an error stating that the function is not defined, even though it is defined in the same class.

import React from 'react'
import Winwheel from 'winwheel'
import TweenMax from 'gsap'

const Roulette = ({ width, height, canvasId}) => {
    let winWheel = null

    const wheelData = {}

    // The issue arises here as alertPrize() function cannot be accessed
    const animation = {
            'type': 'spinToStop',
            'duration': 5,
            'spins': 8,
            'easing': 'Power2.easeInOut',
            'callbackFinished': 'alertPrize()' 
    }

    wheelData['animation'] = animation
    
    function alertPrize()
    {
        createWheel()
        let winningSegment = winWheel.getIndicatedSegment();
        alert("You have won " + winningSegment.text + "!");
    }

    const startAnimation = () => {
        winWheel = null
        createWheel()
        winWheel.startAnimation()
    }

    const createWheel = () => {
        if (typeof window !== 'undefined' && winWheel == null) {
            winWheel = new Winwheel(wheelData)
        }
    }

    createWheel()

    return (
        <div>
            <canvas id={canvasId} width={width} height={height} className='z-0'>
                Canvas not supported
            </canvas>
            <button onClick={startAnimation} className='absolute w-full h-full top-0'></button>
        </div>
    )
}

Is there any way to resolve the issue and access the function on callbackFinished?

Answer №1

Make sure to pass the function name as a reference instead of a string, here is the corrected code snippet.

const animation = {
    'type': 'spinToStop',
    'duration': 5,
    'spins': 8,
    'easing': 'Power2.easeInOut',
    'callbackFinished': alertPrize
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to add to the existing route without replacing in React Router?

Is there a way to append to the route using react router without specifying the complete path? Imagine the current route is /page1/page2 and I want to navigate to /page1/page2/next-page To achieve this, first I need to access the router const router = us ...

Using React Material Design UI with the Routing feature

Currently, I am in the process of developing a simple React application with the assistance of the Material-UI library. After utilizing the create-react-app example provided, I attempted to integrate Router into it for seamless navigation between compone ...

The most effective method for transferring a JavaScript object between a TypeScript frontend and a Node.js backend

I need some advice on how to effectively share a JavaScript object between my Angular2 with Typescript frontend and NodeJS backend in an application I'm working on. Currently, I am using a .d.ts file for the frontend and adding a module.exports in the ...

AWS Amplify is failing to display i18n translations on dynamic pages within Next.js (directory structure)

Currently experimenting with the next-i18next package within a Nextjs project. Encountering an issue specifically on dynamic pages like: /blogs/[id], where i18n seems to struggle to translate the content properly, displaying keys rather than actual transla ...

Utilizing Vue Composables: Effectively Implementing Multiple Instances without State Sharing

In my VueJS application, I have a composable file that fetches information from an API and displays it in a table within two components simultaneously: // Here is a basic example of the composable implementation: export function useDatatable () { const t ...

Having trouble aligning a div in the middle of a slick-slider item using flex styling

I've created a slick slider component in Vue, and I'm facing an issue with centering a circular div inside each of the slider items. Despite trying to align and justify center along with adding margin:auto for horizontal centering, I can't s ...

Only one choice for discriminated unions in react props

Looking to create a typescript type for react component props, specifically a basic button that can accept either an icon prop or a text prop, but not both. My initial attempt with a discriminated union didn't quite produce the desired outcome: inter ...

Using JavaScript regular expressions for email validation criteria

Hey there, I am struggling with Regular Expressions, especially when it comes to client side validation for a specific field. Can you please help me come up with a Regular Expression that would verify if an email address is valid based on these criteria: ...

Crafting a custom look for your TextField with MaterialUI

I am currently utilizing styled components, which is working well for me. The only issue I am facing is that I want to style the border-radius of a specific TextField component. How can I achieve this? import React from 'react'; import TextFie ...

Adjust the color scheme of the menu to match the newly updated background image

On the homepage, there is a background slideshow playing. The issue arises when the menu becomes invisible against a dark background due to its dark font color. It would be ideal for the menu's color to dynamically change with the background. I' ...

Is it necessary to enable validation for an Angular reactive form toggle?

Can you help with this issue I'm having? I have a radio button that asks the user if they have a massage certificate. If they answer yes, a file input should be displayed. By default, the file input should not be required, but if the user selects yes, ...

Guide on transferring the Token from the initial response request to the header of the second request, with the help of Axios in an Ionic React application (Making 2 Post Requests

I am trying to create a user account and save the partner's data simultaneously. The initial axios request is used to create the user and obtain a token in return. I need to pass this token as a header in the second request. Despite implementing &apos ...

What impact does using React state with JSON arrays have on performance?

Currently, as I am delving into learning react, I find myself working on an application that involves a json array stored in a state variable. An interesting observation I made is that React does not trigger a re-render when there are changes to a state va ...

Verify if the header value corresponds

How can I validate the header value in a Node.js application? I want to restrict access to a certain route only if the user includes a specific header and its value matches what is expected. For instance, let's say the route requires a header like Acc ...

Tips for ensuring proper function of bullets in glidejs

I am currently working on implementing glidejs as a slider for a website, but I am facing issues with the bullet navigation. The example on glidejs' website shows the bullets at the bottom of the slider (you can view it here: ). On my site, the bullet ...

Using a Function as an Argument to Return an Unnamed Object

There seems to be a common trend in JavaScript code where a function is passed as a parameter, which in turn returns an anonymous object. myFunction(function() { return { foo: 'bar' }; }); What benefits or reasons are there for u ...

Using material-ui to derive color from the theme

I am looking to utilize a color from my material-ui theme within a component, like so: const MyComponent = props => ( <UsersIcon color={currentTheme.primary1Color} /> ) My goal is to extract a value from the current theme provided. I have co ...

A Guide to Implementing v-for in a JSON Array with Vue.js

After receiving JSON data from the server: [ {"id":"2","name":"Peter","age":"24"}, {"id":"4","name":"Lucy","age":"18"}, ] I am ...

Troubleshooting a problem with jQuery child items

Could someone help me understand why the second div is affected by the last part of the code and not the first? It's puzzling to see all the content disappear, especially when I expected the first div to be impacted as it seems like the immediate pare ...

Using `window.location.href` will terminate any pending asynchronous calls to the API

Before all async calls to the API are completed, window.location.href is triggered when the code runs. Setting a breakpoint on the location resolves the issue. How can I ensure that all calls are finished before invoking window.location.href? Code: const ...