使用React与Onsen UI开发PWA时Tabbar功能异常报错求助
Looks like you're hitting a common pitfall with React-OnsenUI's Tabbar component—those _show/_hide function errors are a dead giveaway that your tab content components aren't set up correctly for Tabbar's internal lifecycle. Let's break down the issue and fix it step by step:
What's Causing the Problem?
OnsenUI's Tabbar relies on the built-in _show and _hide methods from its <Page> component to manage tab transitions. When you pass custom components like ScreenOne and ScreenTwo directly as content, if those components don't use <Page> as their root element, the Tabbar can't access those required methods. This is why you can only switch tabs once before everything breaks.
Step-by-Step Fixes
1. Wrap Your Screen Components in <Page>
Update ScreenOne.js and ScreenTwo.js to have <Page> as their top-level element. For example:
// screenOne.js import React from 'react'; import { Page } from 'react-onsenui'; const ScreenOne = ({ title }) => { return ( <Page title={title}> {/* Your home screen content goes here */} <h1>Welcome to the Home Tab!</h1> </Page> ); }; export default ScreenOne;
Do the exact same for ScreenTwo.js—this ensures the Tabbar can interact with the components using its expected methods.
2. Simplify Your Tabbar Setup
You can clean up the App component code to remove unnecessary props (like passing tabbar or active to your screen components—Tabbar handles these internally):
import React, { Component } from 'react'; import { Tabbar, Tab } from 'react-onsenui'; import 'onsenui/css/onsenui.css'; import 'onsenui/css/onsen-css-components.css'; import ScreenOne from './screenOne'; import ScreenTwo from './screenTwo'; class App extends Component { state = { index: 0, }; handleTabChange = ({ index }) => { this.setState({ index }); }; render() { return ( <Tabbar onPreChange={this.handleTabChange} onPostChange={() => console.log('postChange')} position='bottom' index={this.state.index} renderTabs={() => [ { content: <ScreenOne key="home" title="Home" />, tab: <Tab key="home-tab" label="Home" icon="md-home" /> }, { content: <ScreenTwo key="settings" title="Settings" />, tab: <Tab key="settings-tab" label="Settings" icon="md-settings" /> } ]} /> ); } } export default App;
Notice we also removed the outer <Page> wrapper around Tabbar—Tabbar doesn't need to be nested inside a Page component itself.
3. Verify Dependency Versions
Double-check that your onsenui and react-onsenui versions match in your package.json. Version mismatches can cause unexpected method missing errors. For example, both should be on the same major/minor version (e.g., 2.12.10 for both).
If versions are out of sync, delete your node_modules folder and package-lock.json, then reinstall dependencies:
rm -rf node_modules package-lock.json npm install
Why This Works
By wrapping your screen components in <Page>, you're giving the Tabbar access to the internal methods it needs to manage tab transitions smoothly. Without this, the Tabbar can't properly hide/show tabs after the first click, leading to the errors you're seeing.
内容的提问来源于stack exchange,提问作者Oliver

