GeekyAnts / NativeBase

Mobile-first, accessible components for React Native & Web to build consistent UI across Android, iOS and Web.
https://nativebase.io/
MIT License
20.17k stars 2.39k forks source link

useTheme must be used within NativeBaseConfigProvider #4303

Closed tburnt closed 2 years ago

tburnt commented 2 years ago

Simulator Screen Shot - iPhone 11 - 2021-11-15 at 18 25 56

Firebase.json { "name": "myapp", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest", "lint": "eslint ." }, "dependencies": { "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", "@react-native-community/async-storage": "^1.11.0", "@react-native-community/checkbox": "^0.5.2", "@react-native-community/datetimepicker": "^3.0.2", "@react-native-community/netinfo": "^5.9.5", "@react-native-community/picker": "^1.8.0", "@react-native-community/slider": "^3.0.3", "@react-native-community/toolbar-android": "0.1.0-rc.2", "@react-native-firebase/admob": "^7.6.8", "@react-native-firebase/app": "^8.4.5", "@react-navigation/native": "^6.0.6", "material-design-icons": "^3.0.1", "native-base": "^3.0.0", "nodemailer": "^6.4.14", "patch-package": "^6.4.7", "prop-type": "0.0.1", "react": "16.9.0", "react-native": "^0.66.3", "react-native-calendars": "^1.403.0", "react-native-chart-kit": "^6.6.1", "react-native-color-picker": "^0.6.0", "react-native-device-info": "^5.2.1", "react-native-easy-grid": "^0.2.2", "react-native-elements": "^2.3.2", "react-native-exit-app": "^1.1.0", "react-native-fs": "^2.16.4", "react-native-hr-component": "^2.0.2", "react-native-image-picker": "^2.3.4", "react-native-linear-gradient": "^2.5.6", "react-native-mail": "^5.2.0", "react-native-modal": "^11.5.6", "react-native-modal-datetime-picker": "^9.0.0", "react-native-modal-selector": "^1.1.2", "react-native-month-selector": "^1.4.0", "react-native-month-year-picker": "^1.4.1", "react-native-orientation": "^3.1.3", "react-native-paper": "^3.12.0", "react-native-permissions": "^2.2.2", "react-native-restart": "^0.0.17", "react-native-safe-area-context": "^3.3.2", "react-native-screens": "^2.9.0", "react-native-simple-toast": "^1.0.0", "react-native-svg": "^9.12.0", "react-native-video": "^5.1.0-alpha8", "react-native-video-controls": "^2.6.0", "react-native-video-player": "^0.10.1", "react-native-webview": "^11.14.2", "rn-fetch-blob": "^0.12.0" }, "devDependencies": { "@babel/core": "^7.6.2", "@babel/runtime": "^7.6.2", "@react-native-community/eslint-config": "^3.0.1", "babel-jest": "^27.3.1", "eslint": "^8.2.0", "jest": "^27.3.1", "metro": "^0.66.2", "metro-react-native-babel-preset": "^0.56.0", "moment": "^2.24.0", "react-native-gesture-handler": "^1.4.1", "react-native-reanimated": "^1.2.0", "react-native-render-html": "^4.1.2", "react-native-sqlite-storage": "^4.1.0", "react-native-vector-icons": "^7.1.0", "react-navigation": "^4.4.4", "react-navigation-drawer": "^2.2.2", "react-navigation-stack": "^1.8.1", "react-test-renderer": "16.9.0" }, "jest": { "preset": "react-native" } }

This error is located at: in Container in HomeScreen (created by SceneView) in SceneView (at StackViewLayout.tsx:900) in RCTView (at View.js:32) in Unknown (at createAnimatedComponent.js:242) in AnimatedComponent (at createAnimatedComponent.js:295) in AnimatedComponentWrapper (at StackViewCard.tsx:106) in RCTView (at View.js:32) in Unknown (at createAnimatedComponent.js:242) in AnimatedComponent (at createAnimatedComponent.js:295) in AnimatedComponentWrapper (at src/index.native.js:105) in Screen (at StackViewCard.tsx:93) in Card (at createPointerEventsContainer.tsx:95) in Container (at StackViewLayout.tsx:975) in RCTView (at View.js:32) in Unknown (at src/index.native.js:139) in ScreenContainer (at StackViewLayout.tsx:384) in RCTView (at View.js:32) in Unknown (at createAnimatedComponent.js:242) in AnimatedComponent (at createAnimatedComponent.js:295) in AnimatedComponentWrapper (at StackViewLayout.tsx:374) in PanGestureHandler (at StackViewLayout.tsx:367) in StackViewLayout (created by withOrientation) in withOrientation (at StackView.tsx:104) in RCTView (at View.js:32) in Unknown (at Transitioner.tsx:267) in Transitioner (at StackView.tsx:41) in StackView (created by Navigator) in Navigator (created by KeyboardAwareNavigator) in KeyboardAwareNavigator (created by SceneView) in SceneView (created by Drawer) in RCTView (at View.js:32) in Unknown (created by ResourceSavingScene) in RCTView (at View.js:32) in Unknown (created by ResourceSavingScene) in ResourceSavingScene (created by Drawer) in RCTView (at View.js:32) in Unknown (at src/index.native.js:139) in ScreenContainer (created by Drawer) in RCTView (at View.js:32) in Unknown (at createAnimatedComponent.js:240) in AnimatedComponent(View) (created by Drawer) in RCTView (at View.js:32) in Unknown (at createAnimatedComponent.js:240) in AnimatedComponent(View) (created by PanGestureHandler) in PanGestureHandler (created by Drawer) in Drawer (created by DrawerView) in DrawerView (created by Navigator) in Navigator (created by NavigationContainer) in NavigationContainer (at myapp/index.js:24) in ThemeProvider (created by Provider) in RCTView (at View.js:32) in Unknown (created by Portal.Host) in Portal.Host (created by Provider) in Provider (at myapp/index.js:23) in Main (at renderApplication.js:50) in RCTView (at View.js:32) in Unknown (at AppContainer.js:92) in RCTView (at View.js:32) in Unknown (at AppContainer.js:119) in AppContainer (at renderApplication.js:43) in myapp(RootComponent) (at renderApplication.js:60) ERROR Error: useTheme must be used within NativeBaseConfigProvider

This error is located at: in NavigationContainer (at myapp/index.js:24) in ThemeProvider (created by Provider) in RCTView (at View.js:32) in Unknown (created by Portal.Host) in Portal.Host (created by Provider) in Provider (at myapp/index.js:23) in Main (at renderApplication.js:50) in RCTView (at View.js:32) in Unknown (at AppContainer.js:92) in RCTView (at View.js:32) in Unknown (at AppContainer.js:119) in AppContainer (at renderApplication.js:43) in myapp(RootComponent) (at renderApplication.js:60) ERROR Error: useTheme must be used within NativeBaseConfigProvider

This error is located at: in NavigationContainer (at myapp/index.js:24) in ThemeProvider (created by Provider) in RCTView (at View.js:32) in Unknown (created by Portal.Host) in Portal.Host (created by Provider) in Provider (at myapp/index.js:23) in Main (at renderApplication.js:50) in RCTView (at View.js:32) in Unknown (at AppContainer.js:92) in RCTView (at View.js:32) in Unknown (at AppContainer.js:119) in AppContainer (at renderApplication.js:43) in myapp(RootComponent) (at renderApplication.js:60)

HomeScreen.Java

import React, { Component } from 'react'; import { StyleSheet, View, Text, Image, TouchableOpacity, Linking,ScrollView, TouchableWithoutFeedback, Keyboard, Dimensions, Platform, Clipboard } from 'react-native'; import { TextInput, Snackbar } from 'react-native-paper'; import Toast from 'react-native-simple-toast'; import style from '../styles/CommonLayout'; import GLOBALS from '../customlibs/Globals'; import { DatabaseHelper } from '../customlibs/DatabaseHelper'; import AsyncStorage from '@react-native-community/async-storage'; import { UtilityHelper } from '../customlibs/UtilityHelper'; import LinearGradient from 'react-native-linear-gradient'; var RNFS = require('react-native-fs'); import { Container, Header, Content,Button, Icon, Footer } from 'native-base'; import { Col, Row, Grid } from 'react-native-easy-grid'; import FontAwesome5 from 'react-native-vector-icons/FontAwesome5'; import { Tips } from '../model/Tips'; import Moment from 'moment'; import { BannerAd, BannerAdSize, TestIds } from '@react-native-firebase/admob'; import DeviceInfo from 'react-native-device-info'; import AdBanner from '../layoutelements/AdBanner' import Modal from 'react-native-modal';

const pageStyles = StyleSheet.create({ mainContainer: { flex: 1, marginTop: 5, marginLeft: 15, marginRight: 15, marginBottom: 15 }, modalContent: { backgroundColor: 'white', padding: 5, justifyContent: 'center', borderWidth:4, borderColor: '#000000', borderRadius: 10 }, });

export default class HomeScreen extends Component { constructor(props) { super(props) this.state = { background_color: '#5c5c5c', password: '', tip_today: 0, ads_path: require('../images/banner1.png'), newUpdateSnackbar: false, announcementModalVisible: false, announcements: null }; this.utilHelper = new UtilityHelper(); this.dbHelper = new DatabaseHelper(); this.tipsModel = new Tips(); this.willFocusSubscription; }

componentDidMount(){

Moment.locale('en')
var didLoad = false;
this.willFocusSubscription = this.props.navigation.addListener(
  'didFocus',
  () => {
    didLoad = true;
    this.loadData();

    var path = "";
    if(GLOBALS.ADS_COUNT == 1){
      path = require('../images/banner1.png');
    }else if(GLOBALS.ADS_COUNT == 2){
      path = require('../images/banner2.png');
    }else if(GLOBALS.ADS_COUNT == 3){
      path = require('../images/banner3.png');
    }else if(GLOBALS.ADS_COUNT == 4){
      path = require('../images/banner4.png');
    }
    this.setState( {
      ads_path: path,
    });
    if(GLOBALS.ADS_COUNT == 4){
      GLOBALS.ADS_COUNT = 1;
    }else{
      GLOBALS.ADS_COUNT++;
    }
  }
);

setTimeout(() => {
  if(!didLoad){
    this.loadData();
  }
}, 1000)

var device = (Platform.OS === 'ios') ? 'ios' : 'android';
fetch('http://myurl.com/', {
      method: 'POST',
      headers: new Headers({
          'Content-type': 'application/x-www-form-urlencoded',
      }),
      body: "action=getannouncements&appname=myapp&device=" + device
    }).then((response) => {
      return response.json()
    }).then(async (json)=>{

      console.log(JSON.stringify(json) + "where is this again");
      this.jsondata = [];
      console.log("here001");

      if(json.success) {
        console.log("5555");

        console.log("json.data.lengthhereee 122211: "+ json.data.length);
        for(var i=0;i<json.data.length; i++) {
          if(json.data[i].optionnum != 2 && json.data[i].optionnum != 1) {
            console.log("heree11111");
            this.jsondata.push(json.data[i])
            this.pokeservercount(json.data[i].id)
          }
          else {
            console.log("heree222222");

            var enddate = json.data[i].enddate;
            var annid =  json.data[i].id;
            var rowjson = json.data[i];
            if(json.data[i].optionnum == 1) {
              enddate = 1;
            }
            await this.dbHelper.insertannouncementlog(annid, enddate, rowjson).then((result) => {
              console.log("heree1323232323");
              console.log(result);

              if(result != 0) {
                console.log("appending" + result.id);
                this.jsondata.push(result)
                console.log(JSON.stringify(this.jsondata))
                this.pokeservercount(result.id)
              }
            });
          }
        }
      }
      return this.jsondata;
    }).then((a_data)=>{
      console.log("a_data: "+a_data);
      console.log("a_data: "+a_data.length);
      if(a_data.length > 0) {
        var newArray = []
        a_data.forEach(obj => {
          if(!newArray.some(o=>o.date_modified === obj.date_modified)) {
            newArray.push({...obj})
          }
        });
        //var announcements = this.pubOps.convertTimeFromStampToDisplay(newArray[0].date_modified, true) + "\n\n\n" + newArray[0].announcement
        var announcements = newArray.map((maprecords,o_index) => {
          return (
              <View>
                <View style={{marginBottom: 10}}>
                  <Text style={{fontWeight: 'bold'}}>{this.convertTimeFromStampToDisplay(maprecords.date_modified, true)}</Text>
                </View>
                <View style={{marginBottom: 20}}>
                    <Text>{maprecords.announcement}</Text>
                </View>
              </View>
              )
          })
        }
        if(a_data.length > 0)
          return announcements;
        return 0;
    }).then((result)=>{
      console.log("heresresresresulttttt");
      //console.log(result);
        if(result != 0) {
          setTimeout(()=>{
              this.setState({
              announcementModalVisible: true,
              announcements: result
            })
          }, 1000)
        }
        else {
          this.setState({
            announcementModalVisible: false,
          },()=>{
          });

        }
    }).catch((error) => {
      console.log(error);
    })

}

componentWillUnmount(){ this.willFocusSubscription.remove()

}

pokeservercount(id) { try { AsyncStorage.multiGet(['user_data','background_color'], (err, items) => { if(items[0][1] != '' && items[0][1] != 'undefined' && items[0][1] != null){ var myObject = JSON.parse(items[0][1]); fetch("http://webcoastserver.com/central/public.php",{ method: 'POST', headers: new Headers({ 'Content-type' : 'application/x-www-form-urlencoded', }), body: "action=updateviews&aid="+id+"&uid="+myObject.user_id }).then((response)=>{

      })
    }
  });
} catch(e) {
}

}

convertTimeFromStampToDisplay(timestamp, monthname = true) { return Moment(timestamp, "YYYY-MM-DD HH:mm").format((monthname ? "MMM DD YYYY hh:mm A" : "MM DD YYYY hh:mm A")); }

async loadData() { AsyncStorage.multiGet(['background_color'], (err, items) => { //console.log(items[0][1]) if(items[0][1] != 'undefined' && items[0][1] != null){ this.setState({ background_color: items[0][1] }); } }); var formatdate_sql = Moment(new Date()).format('YYYY/MM/DD'); this.tipsModel.getTipsByQuery("SELECT * FROM "+GLOBALS.TIPS+" where tipdate='"+formatdate_sql+"'") .then((result) => { if(result.length > 0){ this.setState({ tip_today: result[0].tipamt }) } },(err) => {})

this.checkVersion();

}

async checkVersion() { fetch("###########", { method: 'GET', headers: new Headers({ 'Content-Type': 'application/json', // <-- Specifying the Content-Type }) }) .then(response => response.json() ) .then(data => { //console.log(data.android_version) if(Platform.OS == 'ios') {

  }
  else {
    var android_ver = data.android_version;
    if(android_ver > DeviceInfo.getBuildNumber()){
      // this.setState({
      //   newUpdateSnackbar: true
      // })
    }
  }
})
.catch((error) => {
  console.log(error)
});

}

render() { const { checked } = this.state; const {navigate} = this.props.navigation;

return (

  <Container style={{backgroundColor:this.state.background_color}}>
  <Modal
          isVisible={this.state.announcementModalVisible}
          onBackButtonPress={() => {
            this.setState({ announcementModalVisible: false });
          }}
          onBackdropPress={() => {
            this.setState({ announcementModalVisible: false });
          }}
          >
          <View style={pageStyles.modalContent}>
              <ScrollView>
                <View style={{flexDirection: 'row'}}>
                  <Image source={require('../images/app_logo.png')} style={{width: 50, height:50}} />
                  <Text style={{fontSize: 20, fontWeight: 'bold', marginBottom: 30, marginTop: 10, marginLeft: 5}}>
                    Announcements
                  </Text>
                </View>
                <View style={{marginTop: 20}}>
                  {this.state.announcements}
                </View>
              </ScrollView>
            </View>
        </Modal>
    <Snackbar
      visible={this.state.newUpdateSnackbar}
      onDismiss={() => {this.setState({newUpdateSnackbar: false}); this.props.navigation.navigate('SettingsScreen', {}, this.props.navigation.state.params)}}
      >
      New update available
    </Snackbar>
    <Content style={{margin:20}}>

      <Grid>
        <Col style={{height:100}}>
          <Image style={{ width: '100%', height: '100%'}} resizeMode="contain" source={require('../images/money_tray.png')} />
        </Col>
        <Col style={{ justifyContent:'center',height:100 }}>
          <Text style={{color:'white',fontSize:18}}>You have earned</Text>
          <Text style={{color:'#15A246',fontSize:25}}>$ {this.state.tip_today.toFixed(2)}</Text>
          <Text style={{color:'white',fontSize:18}}>Tip for today</Text></Col>
      </Grid>

      <Grid style={{marginTop:35}}>
        <Col style={{height:110,marginRight:5, backgroundColor:'white', alignItems:'center', justifyContent:'center', borderRadius: 5}}>
          <TouchableOpacity onPress={() => this.props.navigation.navigate('EnterTipsScreen', {}, this.props.navigation.state.params) } style={{alignItems:'center', justifyContent:'center'}}>
            <FontAwesome5 style={{marginTop:2,marginLeft:3}} name="edit" size={25} color="#15A246" />
            <Text style={{fontSize:17}}>Enter Tips</Text>
          </TouchableOpacity>
        </Col>
        <Col style={{height:110,marginLeft:5, backgroundColor:'white', alignItems:'center', justifyContent:'center', borderRadius: 5}}>
          <TouchableOpacity onPress={() => this.props.navigation.navigate('SummaryScreen', {}, this.props.navigation.state.params)} style={{alignItems:'center', justifyContent:'center'}}>
            <FontAwesome5 style={{marginTop:2,marginLeft:3}} name="book" size={25} color="#15A246" />
            <Text style={{fontSize:17}}>Summary</Text>
          </TouchableOpacity>
        </Col>
      </Grid>

      <Grid style={{marginTop:15}}>
        <Col style={{height:110,marginRight:5, backgroundColor:'white', alignItems:'center', justifyContent:'center', borderRadius: 5}}>
          <TouchableOpacity onPress={() => this.props.navigation.navigate('CalendarScreen', {}, this.props.navigation.state.params)} style={{alignItems:'center', justifyContent:'center'}}>
            <FontAwesome5 style={{marginTop:2,marginLeft:3}} name="calendar" size={25} color="#15A246" />
            <Text style={{fontSize:17}}>Calendar</Text>
          </TouchableOpacity>
        </Col>
        <Col style={{height:110,marginLeft:5, backgroundColor:'white', alignItems:'center', justifyContent:'center', borderRadius: 5}}>
          <TouchableOpacity onPress={() => this.props.navigation.navigate('ReportsScreen', {}, this.props.navigation.state.params)} style={{alignItems:'center', justifyContent:'center'}}>
            <FontAwesome5 style={{marginTop:2,marginLeft:3}} name="print" size={25} color="#15A246" />
            <Text style={{fontSize:17}}>Reports</Text>
          </TouchableOpacity>
        </Col>
      </Grid>

      <Button onPress={() => this.props.navigation.navigate('SettingsScreen', {}, this.props.navigation.state.params) } block iconLeft style={{marginTop:35,backgroundColor:'#15A246'}}>
        <Icon name='cog' />
        <Text style={{fontSize:17,color:'white'}}>Settings</Text>
      </Button>

    </Content>

    <Footer style={{backgroundColor: '#ffffff00'}}>
      <Grid style={{alignItems: 'center', justifyContent: 'center'}}>
        <Col style={{flexDirection:'row'}}>
          <TouchableOpacity onPress={() => {Linking.openURL("http://webcoastapps.com/privacy-policy/")}}>
            <Text style={{fontSize:15, color:'white', justifyContent:'center', marginLeft: 10}}>Privacy</Text>
          </TouchableOpacity>
        </Col>
        <Col style={{flexDirection:'row', justifyContent: 'flex-end'}}>
          <TouchableOpacity onPress={() => {Linking.openURL("http://webcoastapps.com")}} style={{flexDirection:'row', justifyContent: 'flex-end'}}>
            <Image style={{ width: 35, height: 20}} resizeMode="contain" source={require('../images/wca_logo.png')} />
            <Text style={{fontSize:15,marginLeft:5, color:'white', justifyContent:'center', marginRight: 10}}>Web Coast Apps</Text>
          </TouchableOpacity>

        </Col>
      </Grid>
    </Footer>
    <AdBanner />
  </Container>

);

} }

Actions to duplicate: npm install pod install run in ios

dimtim commented 2 years ago

I get the same error in my project. @tburnt Did you find a solution to work around the problem?

tburnt commented 2 years ago

I get the same error in my project. @tburnt Did you find a solution to work around the problem?

nope I am still burning my candles with this one among all other problems

dimtim commented 2 years ago

@tburnt I've found the source of the problem. There should be always NativeBaseProvider component at the root of Native Base components tree. In my case this component was not the root.

emclab commented 2 years ago

Same error after ugprading to 3.2.2 from 2.15.2. I am using react navigation and not sure where to put NavtiveBaseProvider since react navigation has a root component which is already in App.js.

yousefshakoury commented 2 years ago

I have the same problem "native-base": "^3.2.2", "react": "17.0.2", "react-native": "0.66.3",

rufat commented 2 years ago

I had the same issue since yesterday. The problem was when I create a new app project, I restructured its folder and sub-components. Because of that, I forgot to change the root component's import location in "index.js". I was calling a child component as a root component. Silly me.

So, please double-check that. Otherwise, it will throw the "useTheme" validation error.

tburnt commented 2 years ago

I found out that if you do not put the container tag within a nativebaseprovider tag this will cause that error

EG

/Before declaring the class / const config = { dependencies: { // For Expo projects (Bare or managed workflow) 'linear-gradient': require('expo-linear-gradient').LinearGradient, // For non expo projects // 'linear-gradient': require('react-native-linear-gradient').default, }, };

/In Your Render function/ <NativeBaseProvider config={config}> <Container> / Contents of view here or what you wanna put / </Container> </NativeBaseProvider>

I'm not sure if you have the same solution but this worked for me thanks to @dimtim

HamzaDams commented 2 years ago

I found out that if you do not put the container tag within a nativebaseprovider tag this will cause that error

EG

/Before declaring the class / const config = { dependencies: { // For Expo projects (Bare or managed workflow) 'linear-gradient': require('expo-linear-gradient').LinearGradient, // For non expo projects // 'linear-gradient': require('react-native-linear-gradient').default, }, };

/In Your Render function/ / Contents of view here or what you wanna put /

I'm not sure if you have the same solution but this worked for me thanks to @dimtim

Yes for solve this problem just add component parent NativeBaseProvider in app.js

ZeynepKuy commented 2 years ago

I found out that if you do not put the container tag within a nativebaseprovider tag this will cause that error

EG

/Before declaring the class / const config = { dependencies: { // For Expo projects (Bare or managed workflow) 'linear-gradient': require('expo-linear-gradient').LinearGradient, // For non expo projects // 'linear-gradient': require('react-native-linear-gradient').default, }, };

/In Your Render function/ / Contents of view here or what you wanna put /

I'm not sure if you have the same solution but this worked for me thanks to @dimtim

I have the same problem and tried to solve it like that, but for that you should install the linear gradient first, or? Do you think there is another solution without installing these?

ayyam commented 2 years ago

i got the same error,,

only able to fix by change component as mentioned in official docs https://docs.nativebase.io/radio

export default () => {
  return (
    <NativeBaseProvider>
      <Center flex={1} px="3">
        <Example />
      </Center>
    </NativeBaseProvider>
  )
}

this fix is ok only if i learning the example as same as Docs, but this should not be the case to fix the issue i think..

maocop commented 2 years ago

you have to put first at all, example

nothing here <NativeBaseProvider> All here..... </NativeBaseProvider> nothing here

cryptodev4 commented 2 years ago

If you are working with a main component like App.js and you are using native-base components on all of your app elements, it is a good idea to put the NativeBaseProvider wrapper component at the App.js, if not you can put it on the specific component e.g HomeScreen.js.

nick-0101 commented 2 years ago

I'm getting this error when running jest tests. I've wrapped everything in the <NativeBaseProvider> tag, still not working.

error

nick-0101 commented 2 years ago

I managed to solve that error by wrapping the actual component I was testing in <NativeBaseProvider>

const tree = renderer.create(<NativeBaseProvider><ComponentToTest /></NativeBaseProvider>).toJSON()

neuberoliveira commented 2 years ago

Same here, add NativeBaseProvider like @phoenixbeats01 said partially resolve, because seems my snapshoot tree is empty, like if NativeBaseProvider not render childrens

it('renders correctly', () => {
    const tree = renderer.create(< NativeBaseProvider><PermissionAction text="HAR!!!!" buttonLabel="IAM A BUTTON" buttonAction={()=>null} /></NativeBaseProvider >).toJSON();
    expect(tree).toMatchSnapshot();
  });

and my snapshoot

exports[`Permission Action renders correctly 1`] = `
<RNCSafeAreaProvider
  onInsetsChange={[Function]}
  style={
    Array [
      Object {
        "flex": 1,
      },
      undefined,
    ]
  }
/>
`;

And yes, my PermissionAction renders correctly when run on device

codewithPoppy commented 2 years ago

I faced the same error on my project. Did someone solve this problem?

SupriyaPKalghatgi commented 2 years ago

I get the same error

useTheme must be used within NativeBaseConfigProvider Followed docs https://docs.nativebase.io/testing#page-title

Error

console.error The above error occurred in the <ForwardRef> component: node_modules/native-base/lib/commonjs/utils/styled.js:45:22 node_modules/native-base/lib/commonjs/components/primitives/Input/InputBase.tsx:111:31 node_modules/native-base/lib/commonjs/components/primitives/Input/Input.tsx:123:10 node_modules/native-base/lib/commonjs/components/composites/Toast/Toast.tsx:240:7

@surajahmed Please review this

SupriyaPKalghatgi commented 2 years ago

@rayan1810 Can you check this please

JambaGoDevCode commented 2 years ago

Visite...

https://stackoverflow.com/a/73043985/17818034

robbporto commented 2 years ago

Any news on this? I simply cannot test anything on my app because of this issue.

JambaGoDevCode commented 2 years ago

Any news on this? I simply cannot test anything on my app because of this issue.

I've solve this... click on link

https://stackoverflow.com/a/73043985/17818034

surajahmed commented 2 years ago

@dimtim Thanks for the solution.

  1. NativeBaseProvider should be at the root level of the react tree.
  2. We cannot use useTheme hook (other NativeBase hooks as well) outside NativeBaseProvider. Our App.js should look like this
import React from "react";
// 1. import `NativeBaseProvider` component
import { NativeBaseProvider, Text, Box } from "native-base";

function Example () {
    const {theme} = useTheme(); // This will work fine as it is inside NativeBaseProvider
    return <Box flex={1} bg="#fff" alignItems="center" justifyContent="center">
            <Text>Open up App.js to start working on your app!</Text>
          </Box>
}
export default function App() {
  // 2. Use at the root of your app
  // 3. We cannot use useTheme here. This will throw an error.
  return (
    <NativeBaseProvider>
       <Example/>
    </NativeBaseProvider>
  );
}

Going to close this issue. Please feel free to reopen if you face any problems. Thanks ❤️

MrVico commented 2 years ago

I'm still facing the issue regarding the tests...

The app runs fine and everything since I wrapped the App.js content inside the NativeBaseProvider tag but when running unit tests against React components I get the useTheme must be used within NativeBaseConfigProvider error. I then wrapped the render of the tested component inside the NativeBaseProvider in the test file but that just changes the error to something not very understandable.

How did you guys manage to test your code?

JambaGoDevCode commented 2 years ago

Hi,

MrVico @.***> escreveu no dia segunda, 12/09/2022 à(s) 18:04:

I'm still facing the issue regarding the tests...

The app runs fine and everything since I wrapped the App.js content inside the NativeBaseProvider tag but when running unit tests against React components I get the useTheme must be used within NativeBaseConfigProvider error. I then wrapped the render of the tested component inside the NativeBaseProvider in the test file but that just changes the error to something not very understandable.

How did you guys manage to test your code?

— Reply to this email directly, view it on GitHub https://github.com/GeekyAnts/NativeBase/issues/4303#issuecomment-1244032492, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANC75SCTP7OEC5W4XI5BYFDV55PCBANCNFSM5IBKS66Q . You are receiving this because you commented.Message ID: @.***>

sarnakov commented 1 year ago

@surajahmed Please, reopen the issue, @MrVico is right there are still errors when you try to write tests.

IlirBajrami commented 1 year ago

@dimtim Thanks for the solution.

  1. NativeBaseProvider should be at the root level of the react tree.
  2. We cannot use useTheme hook (other NativeBase hooks as well) outside NativeBaseProvider. Our App.js should look like this
import React from "react";
// 1. import `NativeBaseProvider` component
import { NativeBaseProvider, Text, Box } from "native-base";

function Example () {
    const {theme} = useTheme(); // This will work fine as it is inside NativeBaseProvider
    return <Box flex={1} bg="#fff" alignItems="center" justifyContent="center">
            <Text>Open up App.js to start working on your app!</Text>
          </Box>
}
export default function App() {
  // 2. Use at the root of your app
  // 3. We cannot use useTheme here. This will throw an error.
  return (
    <NativeBaseProvider>
       <Example/>
    </NativeBaseProvider>
  );
}

Going to close this issue. Please feel free to reopen if you face any problems. Thanks ❤️

Gives error when testing: useTheme must be used within NativeBaseConfigProvider Please re-open!

IlirBajrami commented 1 year ago

To fix the testing issue, you can simply pass initialWindowMetrics to NativeBaseProvider in your tests.

const inset = {
  frame: { x: 0, y: 0, width: 0, height: 0 },
  insets: { top: 0, left: 0, right: 0, bottom: 0 },
};

<NativeBaseProvider initialWindowMetrics={inset}>
  {children}
</NativeBaseProvider>
valendres commented 1 year ago

Like others in this thread, I have been countering the following error within my Jest unit tests:

`useTheme` must be used within `NativeBaseConfigProvider`

There's a lot of posts saying to just add a "NativeBaseProvider" which is all that's required in most cases, but depending on the repo setup, this may or may not work.

If you're utilising a pnpm or yarn monorepo that imports native-base in multiple packages, it's possible that there is more than 1 copy of native-base installed within the various node_modules folders. This can incorrectly result in different versions of the NativeBaseConfig context being used.

Thankfully, we can force Jest to resolve the same version of native-base, regardless of where it is being imported by adding the following to jest.config.js:

const path = require('path');

/** @type {import('jest').Config} */
module.exports = {
  ...
  moduleNameMapper: {
    'native-base': path.join(__dirname, '../../node_modules/native-base'),
  },
};

For some additional context, I'm utilising a pnpm monorepo with the following structure:

monorepo root
    node_modules
        native-base <-- hoisted version of the library that's shared by multiple packages/apps
    packages
        components <-- custom components/molecules
            node_modules
                native-base <-- a version of the library that's specific to `components`
            src
                Potato.tsx <-- uses `native-base`
        feature-auth
            src
                LoginForm.tsx <-- utilises `Potato` from local `components` package
                LoginForm.test.tsx

It was in my LoginForm.test.tsx where I encountered the error, specifically within the Potato component. One might expect that simply adding a NativeBaseProvider in the LoginForm.test.tsx would solve the issue, but it didn't. The reason is that a different version of the Context was being loaded.

Even though they have the same name, they're actually utilising completely different versions of the library. Which explains why the Context.Consumer within useTheme is unable to find the Context.Provider. By adding the jest config above, we can force the native-base to be imported from the root node-modules folder in both feature-auth and components 🎉

trajano commented 1 year ago

One thing I found was that there were two copies of native-base on my monorepo so you have to ensure that all components resolve to the same version.

norellanac commented 1 year ago

This works for me https://docs.nativebase.io/testing#page-title

MyTestfile-test.tsx

import 'react-native';
import React from 'react';
import { Test } from '../screens/Test';

import { render } from '@testing-library/react-native';
import { NativeBaseProvider } from 'native-base';

const inset = {
  frame: { x: 0, y: 0, width: 0, height: 0 },
  insets: { top: 0, left: 0, right: 0, bottom: 0 },
};

it('renders correctly', () => {
  const wrapper = render(
    <NativeBaseProvider initialWindowMetrics={inset}>
      <Test />
    </NativeBaseProvider>,
  );
  wrapper.findByText('Test');
});

Test.tsx

import { Text } from 'native-base';
import React from 'react';

export const Test = () => {
  return <Text testID='test'>Test</Text>;
};
helloworld-Andrew commented 1 year ago

Same here, add NativeBaseProvider like @phoenixbeats01 said partially resolve, because seems my snapshoot tree is empty, like if NativeBaseProvider not render childrens

it('renders correctly', () => {
    const tree = renderer.create(< NativeBaseProvider><PermissionAction text="HAR!!!!" buttonLabel="IAM A BUTTON" buttonAction={()=>null} /></NativeBaseProvider >).toJSON();
    expect(tree).toMatchSnapshot();
  });

and my snapshoot

exports[`Permission Action renders correctly 1`] = `
<RNCSafeAreaProvider
  onInsetsChange={[Function]}
  style={
    Array [
      Object {
        "flex": 1,
      },
      undefined,
    ]
  }
/>
`;

And yes, my PermissionAction renders correctly when run on device

Hi, This answer works for me. You can try it. Here is the link. https://github.com/GeekyAnts/NativeBase/issues/3907#issuecomment-919037353

gkasireddy202 commented 5 months ago
> Same here, add `NativeBaseProvider` like @phoenixbeats01 said partially resolve, because seems my snapshoot tree is empty, like if NativeBaseProvider not render childrens > ```js > it('renders correctly', () => { > const tree = renderer.create(< NativeBaseProvider>null} />).toJSON(); > expect(tree).toMatchSnapshot(); > }); > ``` > > > > > > > > > > > > and my snapshoot > ``` > exports[`Permission Action renders correctly 1`] = ` > onInsetsChange={[Function]} > style={ > Array [ > Object { > "flex": 1, > }, > undefined, > ] > } > /> > `; > ``` > > > > > > > > > > > > And yes, my `PermissionAction` renders correctly when run on device Hi, This answer works for me. You can try it. Here is the link. [#3907 (comment)](https://github.com/GeekyAnts/NativeBase/issues/3907#issuecomment-919037353)

@helloworld-Andrew - Can you please provide a sample example with full code.?

gkasireddy202 commented 5 months ago

Same error after ugprading to 3.2.2 from 2.15.2. I am using react navigation and not sure where to put NavtiveBaseProvider since react navigation has a root component which is already in App.js.

@emclab - Same problem.Are you fixed this issue?

rayan1810 commented 5 months ago

Same error after ugprading to 3.2.2 from 2.15.2. I am using react navigation and not sure where to put NavtiveBaseProvider since react navigation has a root component which is already in App.js.

@emclab - Same problem.Are you fixed this issue?

Just wrap your React Navigation Provider/Root Component with NativeBaseProvider

gkasireddy202 commented 5 months ago

Same error after ugprading to 3.2.2 from 2.15.2. I am using react navigation and not sure where to put NavtiveBaseProvider since react navigation has a root component which is already in App.js.

@emclab - Same problem.Are you fixed this issue?

Just wrap your React Navigation Provider/Root Component with NativeBaseProvider

@rayan1810 - I am using react-navigation version 4 in the project.Any suggestions?

rayan1810 commented 5 months ago

Wrap the root of your App with the NativeBaseProvider.

gkasireddy202 commented 5 months ago

Wrap the root of your App with the NativeBaseProvider.

@rayan1810 - I am using the "native-base": "^2.15.2", in my project has react-native version:0.73.2. I need to update the native-base without NativeBaseProvider.My client does not need the header style. Any suggestion, please.

rayan1810 commented 5 months ago

You can't upgrade NativeBase without NativeBaseProvider

gkasireddy202 commented 5 months ago

You can't upgrade NativeBase without NativeBaseProvider

Can you please suggest any other version without NativeBaseProvider?