Skip to content

React functional component - Cannot read property 'tokenize' of undefined #20

@HOllarves

Description

@HOllarves

Hi!

I'm trying to implement your lib in a React functional component.

However I'm getting a Cannot read property 'tokenize' of undefined error when attempting to get the reference for the tokenize function.

Here's a summary of my code:

import React, { useEffect, useState } from 'react'
// Braintree
import { Braintree, HostedField } from 'react-braintree-fields'

const Checkout = ({ location }) => {
    const [tokenize, setTokenizeFunc] = useState(null)
    return (
        <Braintree
                    className={isReady ? '' : 'disabled'}
                    authorization='sandbox_xxxxxxxxxxxxx'
                    onAuthorizationSuccess={onAuthorizationSuccess}
                    onDataCollectorInstanceReady={onDataCollectorInstanceReady}
                    onError={handleError}
                    onCardTypeChange={onCardTypeChange}
                    getTokenRef={ref => setTokenizeFunc(ref)}
                    styles={{
                        'input': {
                            'font-size': 'inherit',
                        },
                        ':focus': {
                            'color': 'blue'
                        },
                    }}>
                        [...]
        </Braintree>
    )
}

I've also tried doing something like getTokenRef={ref => setTokenizeFunc.bind(Braintree, ref)} but still no luck.

Is this implementation possible? Or should I just stick to class components in this scenario?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions