Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property '_root' of null in React Native when using NativeBase Toast component

I'm using NativeBase's Toast component to show a message after successfully submitting a form, but I get this error message in the console every time I run it.

_onSubmit error TypeError: Cannot read property '_root' of null
    at Function.show (ToastContainer.js:79)
    at InventoryItemAdd.js:40
    at tryCallOne (core.js:37)
    at core.js:123
    at JSTimers.js:98
    at Object.callTimer (JSTimersExecution.js:95)
    at Object.callImmediatesPass (JSTimersExecution.js:199)
    at Object.callImmediates (JSTimersExecution.js:214)
    at MessageQueue.js:222
    at guard (MessageQueue.js:46)

Here's the JSX in my render function:

<Container>
    <Navbar title="Add an Inventory Item" backArrow />
    <Content keyboardShouldPersistTaps="handled">
        <InventoryItemAddForm
            onSubmit={this._onSubmit}
            data={formData}
            enableReinitialize={true}
            initialValues={initialValues}
        />
    </Content>
    <FooterTabs />
</Container>

And my _onSubmit function:

_onSubmit = ({name, inventoryTypeId, internalCode, description = "", measurementUnitId}) => {
    const {inventoryItemCreate, showErrors} = this.props

    return inventoryItemCreate({
        input: {
            name: name.trim(),
            inventoryTypeId,
            internalCode: internalCode.trim(),
            description: description.trim(),
            measurementUnitId,
        }
    })
        .then(({data: {inventoryItemCreate}}) => {
            dismissKeyboard()
            Toast.show({
                text: "Inventory item added successfully!",
                position: "bottom",
                buttonText: "Okay",
                type: "success",
                duration: 2000,
            })
            Actions.InventoryItemDetails({id: inventoryItemCreate.id})
        })
        .catch((error) => {
            console.log('_onSubmit error', error)
            showErrors()
        })
}
like image 669
Christopher Bradshaw Avatar asked Dec 19 '22 08:12

Christopher Bradshaw


1 Answers

I found out the root component of the app needs to be wrapped in nativebase's <Root> component in order for Toast notifications to work reliably.

like image 152
Christopher Bradshaw Avatar answered Dec 24 '22 01:12

Christopher Bradshaw