javascript - 使用 NativeBase Toast 组件时,无法在 React Native 中读取 null 的属性 '_root'

标签 javascript react-native react-native-ios native-base

我正在使用 NativeBase 的 Toast 组件在成功提交表单后显示一条消息,但每次运行它时我都会在控制台中收到此错误消息。

_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)

这是我的渲染函数中的 JSX:

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

还有我的_onSubmit 函数:

_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()
        })
}

最佳答案

我发现应用程序的根组件需要包装在 nativebase's <Root> component 中为了让 Toast 通知可靠地工作。

关于javascript - 使用 NativeBase Toast 组件时,无法在 React Native 中读取 null 的属性 '_root',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43461617/

相关文章:

javascript - 使用 axios 将数据发送到输入 ReactJS 中的每个更改

javascript - 字符串不能强制转换为Accessibility委托(delegate)可访问性角色

react-native - 如何在 react-native 中渲染 epub 文件

react-native - 如何在 React Native IOS 应用程序中发出 HTTP 请求?

javascript - 使多段线与传单中的道路对齐

javascript - jQuery 脚本无法正常运行

javascript - jquery 滚动效果不适用于 iphone

javascript - react native 错误 : Element Type is Invalid

reactjs - 如何在 Android 上设置 React Native 的日志记录级别?

react-native - 在 React Native V 60+ 中渲染 Open-sans 字体时出现问题