React Native Tutorial #25 - Custom Header Component

Поделиться
HTML-код
  • Опубликовано: 14 ноя 2024

Комментарии • 156

  • @raphaelspitz3682
    @raphaelspitz3682 4 года назад +53

    for the problem of heigth and witdht 100% just had in header.js :
    import React from 'react';
    import { StyleSheet, Text, View } from 'react-native';
    import { MaterialIcons } from '@expo/vector-icons';
    import { Dimensions } from 'react-native';
    export default function Header() {
    return (

    {/* icon for the menu */}
    GameZone

    )
    }
    const styles = StyleSheet.create({
    header: {
    width:Dimensions.get('screen').width,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: '#333',
    letterSpacing: 1,
    }
    });

    • @Bentengineering
      @Bentengineering 4 года назад +4

      I left height: '100%' and this worked perfectly. Thank you!

    • @sim_scape
      @sim_scape 4 года назад

      Perfect

    • @chengchao0311
      @chengchao0311 4 года назад +11

      i added "width:Dimensions.get('screen').width" , but the position of header title appeared has a left padding on Android simulator. Then i fixed it by adding " headerTitleAlign: 'center' " for Home Screen navigationOptions.

    • @michaellesley2146
      @michaellesley2146 4 года назад

      Obrigado!,

    • @r27td48
      @r27td48 4 года назад

      Amazing advices, just a doubt, how can I change the side of the default arrow on the top? change the side of the arrow to go back to the previous page ...

  • @maximebarber3780
    @maximebarber3780 4 года назад +21

    Hey gang, here is a solution I implemented for React Navigation 5
    import React from 'react';
    import { createStackNavigator } from '@react-navigation/stack';
    import Home from '../screens/home';
    import ReviewDetails from '../screens/reviewDetails';
    import Header from '../shared/header';
    const homeStack = ({ navigation }) => {
    const Stack = createStackNavigator();
    return (
    ,
    }}
    />

    );
    };
    export default homeStack;

  • @ImranSheikh-kg4qd
    @ImranSheikh-kg4qd 4 года назад +54

    width: '100%' and height: '100%' not Working in the new version of React Navigation.
    But i manage to fix it ,
    add -- headerTitleAlign: "center" in "Home" Screen options (homeStack.js)
    then add height: '100%' this will fix height. {you can give a custom height value instead of these}.
    and width: Dimensions.get('window').width . Don't forget to import Dimensions from 'react-native'
    Note: this is a fix not a solution . This could break other thing . Share the real solution if you get it.

    • @lulzekontube3577
      @lulzekontube3577 4 года назад +1

      I love you

    • @ruset-dewss4698
      @ruset-dewss4698 4 года назад +1

      Your method solves the issue, there are obviously many methods but yours was the simplest and it Works!

    • @michaellesley2146
      @michaellesley2146 4 года назад

      @@ruset-dewss4698 Obrigado!

    • @zeeshanalikhan8258
      @zeeshanalikhan8258 3 года назад

      yes exactly, i have created special component for width and height, and i exported them wherever i required it

    • @vivianeb90
      @vivianeb90 3 года назад

      Has this been fixed in newer versions of react navigator? Because I am using version 6 and having the issue that my whole custom nav component inside header is shifted towards the right by about 5-10 pixels and I have no idea why because I have the same exact component outside my header as comparison and it shows as it should, no styling difference. So I think that it must come from some defaults in header styles that I don't understand yet.

  • @piusvictor8780
    @piusvictor8780 3 года назад +1

    Your level of kungfu to break web concepts is more than awesome, am watching your tutorial while smiling, you deserve trillion subscribers. Be blessed man.

  • @joeuser9690
    @joeuser9690 4 года назад +2

    For those of you doing this in React Navigation 5 here is the way I used the Header component on the homeStack: }} />

    • @robinware77
      @robinware77 4 года назад

      reactnavigation.org/docs/headers/#setting-the-header-title here is the link to the documentation in case anyone wants to read it.

    • @lawp3537
      @lawp3537 2 года назад

      Thank you, I was look for this

  • @wilsoncardoso150
    @wilsoncardoso150 2 года назад +2

    The latest version of DrawerNavigator already comes with a header and a burger menu icon.

  • @cedrickvstheworld1810
    @cedrickvstheworld1810 4 года назад +1

    I can actually create a production grade mobile app from here! Thanks man!

  • @DionnyPrensa
    @DionnyPrensa 4 года назад +1

    You can use the HOC withNavigation. This HOC "inject" the navigation prop from the nearest parent.
    import {withNavigation} from "react-navigation";
    function Header({navigation}) {}
    export default withNavigation(MyHeader);
    You don't need pass the navigation prop for every child or grandson

    • @akash-kumar737
      @akash-kumar737 4 года назад

      Thanks 😊. Need to install another package for v5.

  • @pratp3889
    @pratp3889 4 года назад +3

    i like this tutorial, but please mention the version of package you install so that we can still create app as mentioned in course without any version issues in future. :)

  • @theweebdev
    @theweebdev 4 года назад

    Finally! I've been waiting for this since last year. Thanks and you are awesome!

  • @MustafaAgaDev
    @MustafaAgaDev 4 года назад +2

    Here is my workaround about the header style:
    import React from 'react'
    import { View, StyleSheet, Text, Dimensions } from 'react-native';
    import {MaterialIcons} from '@expo/vector-icons';
    export default function Header({navigation, title}) {
    const openMenu =()=>{
    navigation.openDrawer();
    }
    return (




    {title}



    )
    }
    const styles = StyleSheet.create({
    header: {
    width:Dimensions.get('screen').width,
    height: '100%',
    flexDirection: 'row',
    flex:1,
    alignItems: 'center',
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: '#333',
    letterSpacing: 1,
    height: '100%',
    alignItems:"center",
    },
    icon: {
    position: 'relative',
    flexDirection:'column',
    flex:0.5
    }
    });

    • @chengchao0311
      @chengchao0311 4 года назад +2

      i added "width:Dimensions.get('screen').width" , but the position of header title appeared has a left padding on Android simulator. Then i fixed it by adding " headerTitleAlign: 'center' " for Home Screen navigationOptions.

    • @luisguillermo8375
      @luisguillermo8375 4 года назад +3

      By any chance do you have the code for homeStack.js? I'm having problems opening the drawer function with the icon, and I think it has something to do with headerTitle:

    • @zuhryfayesz-me6uy
      @zuhryfayesz-me6uy 4 года назад +1

      This worked for me for my iPhone 11 expo UI .. Thanks ..

  • @muhammadzulqarnan533
    @muhammadzulqarnan533 3 года назад

    if header background color not fill ..then check you have already set background color in
    homeStack.js
    defaultNavigationOptions:{
    headerTintColor:'#444',
    headerStyle:
    { backgroundColor:'red', height:60 }
    }

  • @jonathanhirshowitz722
    @jonathanhirshowitz722 4 года назад

    header won't be centered, but at least the icon and header won't be stacked and the solution is
    a lot simpler. just remove several styling rules:
    const styles = StyleSheet.create({
    header: {
    flexDirection: 'row'
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: '#333',
    letterSpacing: 1
    }
    })

  • @mahdinevisandeh833
    @mahdinevisandeh833 Год назад

    In react navigation V.6 it automatically adds the menu icon and there's no need to go all this long way just to open drawer with a button. Though it's understandable that this video is for almost 3 years ago and so, I don't mean that it's Mr Shaun's fault!

  • @harikaprasanna
    @harikaprasanna 4 года назад

    hey thanks alot ,i have solved my navigation issue ...12:10 good explaination...

  • @danielstoicamusic
    @danielstoicamusic 4 года назад +1

    Welcome back, Shaun!
    Question for you: where can we find more info about this solution (and maybe other solutions) for giving the custom header access to navigation?
    Also, would this have worked by using withNavigation somehow?

  • @techtian4351
    @techtian4351 4 года назад

    bro, where were you? missed your tuts, glad you're back

  • @robsonsilv4.
    @robsonsilv4. 4 года назад +1

    If someone have problems with configuration, try to stick using lib versions same as the course, like expo 35 e etc. I'm having to fix some major problems on actual versions.

  • @kensyjolicoeur
    @kensyjolicoeur 4 года назад +1

    I like this one "we are going to do that in a second".

  • @filankzahmoul3494
    @filankzahmoul3494 2 года назад +3

    LATEST VERSION CODE 01/06/2022 --------->>>>>>>>
    THIS IS THE ABOUTSTACK FILE , JUST DO THE SAME IN HOMESTACK FILE
    import { createNativeStackNavigator } from '@react-navigation/native-stack'
    import Header from '../shared/Header'
    import About from '../screens/About'
    const Stack = createNativeStackNavigator()
    const AboutStack = ({ navigation }) => {
    return (
    ,
    }}
    />

    )
    }
    export default AboutStack
    AND THIS IS THE HEADER COMPONENT----------->>>>>>>>>>
    import React from 'react'
    import { View, Text, StyleSheet } from 'react-native'
    import { MaterialIcons } from '@expo/vector-icons'
    const Header = ({ navigation ,title}) => {
    const openMenu = () => {
    navigation.openDrawer()
    }
    return (



    {title}


    )
    }
    const styles = StyleSheet.create({
    header: {
    height: '100%',
    width: '100%',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    letterSpacing: 1,
    color: 'white',
    },
    icon: {
    position: 'absolute',
    left: 10,
    color: '#fff',
    },
    })
    export default Header

    • @ojitelikenechukwu3895
      @ojitelikenechukwu3895 2 года назад

      ya a life saver

    • @Gamerfan2000
      @Gamerfan2000 2 года назад +1

      Can you please explain when you said "DO ThE SAME FOR HOMESTACK"? Also, could you please post the code for App.js and Drawer.js. I'm having too much trouble getting the icon for opening to get to work.

  • @kensyjolicoeur
    @kensyjolicoeur 4 года назад

    at 3:27 set { flex:1 } to make it fill the header instead of { height:'100%' , width:'100%' }

    • @osamagamal495
      @osamagamal495 4 года назад

      still doesn't work, the header doesn't take the whole space.

    • @osamagamal495
      @osamagamal495 4 года назад

      can You send snippet of the code?

    • @ImranSheikh-kg4qd
      @ImranSheikh-kg4qd 4 года назад

      @@osamagamal495 flex:1 - - not working for me also

    • @osamagamal495
      @osamagamal495 4 года назад

      ​@@ImranSheikh-kg4qd Yes. I think the problem isn't related to the code, It has something to do with installed packages, because I cloned his Github link, and It works perfectly fine.

    • @ImranSheikh-kg4qd
      @ImranSheikh-kg4qd 4 года назад

      @@osamagamal495 yes,Its happening for the New Version of React Navigation

  • @TheNerdyDev
    @TheNerdyDev 4 года назад

    2020 First Ninja Video. 😀

  • @ahmaddawood9691
    @ahmaddawood9691 3 года назад

    You r a Savior Ninja....you are on the top on many famous instructors at UDEMY ....Trust me
    T H A N K S

  • @r27td48
    @r27td48 4 года назад +1

    Amazing advices, just a doubt, how can I change the side of the default arrow on the top? change the side of the arrow to go back to the previous page ...

  • @naveengoyal5243
    @naveengoyal5243 4 года назад

    Finally......you are back again :)

  • @aldodoohan7917
    @aldodoohan7917 4 года назад +1

    hello guys , maybe for you who just watching this recently will find that your title and icon are not on the right place , so i've been looking for the answer and i finally fix this . First i will copy Mykhailo Stepanov comment , you should change your navigationOptions on your home/aboutStack with this navigationOptions: ({ navigation }) => {
    return {
    header: () =>
    }
    }
    and the second is , move your styles.header into your inner view , don't put styles.header as a wrapper(outer view) . Just adjust the rest , Goodluck
    #note ,you should add background color on your wrapper

  • @pauliusgaizauskas7416
    @pauliusgaizauskas7416 4 года назад +3

    full solution with React Navigation 5, and hooks:
    github.com/Paulius11/reactNativeTutorial/tree/lesson-25

  • @silviovieira7724
    @silviovieira7724 4 года назад +1

    Please, sir, show us what is the best way to solve the width/heigh problem.

  • @guymross
    @guymross 3 года назад

    Thank you so much!

  • @thenoob8682
    @thenoob8682 3 года назад

    you are the best

  • @muftaut.8076
    @muftaut.8076 4 года назад

    Brilliant job!!!

  • @VS257
    @VS257 3 года назад +3

    What if u already have a burger icon for the drawer? The new React navigation has both the stack and drawer header on top of each other! Please help!

  • @muhammadzulqarnan533
    @muhammadzulqarnan533 3 года назад

    in header Stylesheet width:'100%', height:100%' work fine ' for react-native cli version v4
    code is below
    const styles=StyleSheet.create({
    header:{

    width:'100%',
    // width:Dimensions.get('screen').width,
    height:'100%',

    flexDirection:'row',
    alignItems:'center',
    justifyContent:'center',
    // background color already set in defaultNavigationOptions no need here
    //backgroundColor:'red',
    },
    headerText:{
    fontWeight:'bold',
    fontSize:20,
    color:'#333',
    letterSpacing:1,
    }
    });

  • @Isaac-xc2hc
    @Isaac-xc2hc 4 года назад +2

    import React from 'react';
    import { StyleSheet, Text, View} from 'react-native';
    import { MaterialIcons } from '@expo/vector-icons';
    import { Dimensions } from 'react-native';
    export default function Header( {title,navigation} ) {
    const openMenu = () => {
    navigation.openDrawer();
    }
    return (

    {title}

    );
    }
    const styles = StyleSheet.create({
    header: {
    width:Dimensions.get('screen').width,
    height: '100%',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: '#fff',
    letterSpacing: 1,
    },
    icon: {
    position: 'absolute',
    left: 16,
    }
    });

  • @tubehelpr
    @tubehelpr 4 года назад +2

    React-navigation V5 will not work with this, unfortunately. Great tutorial though!

  • @SaintPepsiSanCoca
    @SaintPepsiSanCoca 4 года назад +2

    Does your app crash? changing `width:100%, height:100%` to `flex:1` fixed it for me.

  • @MrBibipkins
    @MrBibipkins 4 года назад +13

    setting width and height to 100% does not make header to take all the space. does anyone have the same problem? expo sdk-36

    • @skomsdungeon
      @skomsdungeon 4 года назад

      I have the same problem :( anyone got a solution for this?

    • @andriiielagin2243
      @andriiielagin2243 4 года назад

      And I have the same issue

    • @andriiielagin2243
      @andriiielagin2243 4 года назад +1

      @@skomsdungeon It is possible to add left margin to the headerText style, for example marginLeft: 60

    • @raphalugs
      @raphalugs 4 года назад +5

      If you want to center the headerText, you should go to homeStack.js and after headerStyle you put this headerTitleAlign: "center" (do as the same in aboutStack.js). But I don't know how to put the hamburger icon to left of the screen. Until now, I haven't solved the 100% dimensions problem :/

    • @ДмитрийИванов-з8з2м
      @ДмитрийИванов-з8з2м 4 года назад

      windows or mac os ?

  • @張嘉達-c7x
    @張嘉達-c7x 4 года назад

    instead of passing navigation object as props to Header, i use useNavigation() hook in Header Component, it still work perfectly. Does it make sense ?

  • @manoj-k
    @manoj-k 3 года назад

    🔥🔥🔥

  • @asadabdulmajid6270
    @asadabdulmajid6270 4 года назад +10

    My header is not going to the center.???

    • @abitcompanyco2873
      @abitcompanyco2873 4 года назад

      The same happens to me. Please help me, how do I fix it?

    • @2h4d0wDK
      @2h4d0wDK 4 года назад

      I have the same problem, seems like it's because some things were added to the later versions of the libraries since this video has come out/was filmed.
      I still haven't found a fix working yet.

    • @2h4d0wDK
      @2h4d0wDK 4 года назад +7

      adding ´´ headerTitleAlign: "center" ´´ to defaultNavigationOptions in both aboutStack and homeStack seems to have fixed the centering of the title text for some.

    • @blumaa
      @blumaa 4 года назад

      @Rex Otavotoma Just wanted to add that Rex's solution worked for me. Also, I forget to import { Dimensions } from 'react-native'. After I did this everything worked.

  • @arihanthirawat6574
    @arihanthirawat6574 4 года назад

    please make a video on expo eject

  • @pramithwijethunga2161
    @pramithwijethunga2161 3 года назад

    Plz sir can you do a modern react native tutorial with react-native cli..?? I learned a lot with the help of your tutorials. please be kind enough to consider my request.

  • @pandaman6146
    @pandaman6146 4 года назад

    The header's height and width do not behave normal.
    I used [header] instead of [headerTitle] in homeStack.js? Is it correct?
    const screen={
    Home:{
    screen:Home,
    navigationOptions:({navigation})=>({
    header:()=>
    })
    }
    }

  • @sarlaysid4830
    @sarlaysid4830 4 года назад

    MaterialIcons didnt work for me (I dont know why) but I tried it with SimpleLineIcons and it worked

  • @vijaytiwari826
    @vijaytiwari826 4 года назад +6

    React Navigation v5.x
    // Changes in routes/homeStack.js
    import React from 'react';
    import { createStackNavigator } from '@react-navigation/stack';
    import { MaterialIcons } from '@expo/vector-icons';
    import Home from '../screens/home';
    import ReviewDetails from '../screens/reviewDetails';
    const { Navigator, Screen } = createStackNavigator();
    export const HomeStack = ({ navigation }) => {
    const openMenu = () => {
    navigation.openDrawer();
    }
    return (

    (

    ),
    headerLeftContainerStyle: {
    paddingHorizontal: 20,
    }
    }}
    />


    );
    };
    export default HomeStack;
    // Similar changes in routes/aboutStack.js

    • @emmuoriginals5296
      @emmuoriginals5296 3 года назад +1

      Thanks my guy

    • @andressalomon6703
      @andressalomon6703 3 года назад +1

      Thanks Vijay! This also worked in RN v6.x. I think is a great way of doing this custom header, as you don't need to create a new component.

  • @tonikevo1333
    @tonikevo1333 3 года назад

    My screen freezes when going from DrawerNavigation to About and back to Home. Is there any solution?

  • @milovanlacarak8959
    @milovanlacarak8959 4 года назад +1

    not judging but I think you are making it less readable by passing objects instead of just using const DrawerName and then making a componenet, its much more presentable that way, Im learning React and most people probably look into docs, docs stick with the convention I stated the way you do it is just confusing for someone trying to learn it

  • @salahalsamarraaee650
    @salahalsamarraaee650 2 года назад

    thank you

  • @ankitpradhan4183
    @ankitpradhan4183 4 года назад

    No matter what I still do not get the openDrawer() to work. I am getting openDrawer from navigation in props, but still can not toggle it,.

  • @charlescampista9384
    @charlescampista9384 3 года назад

    What if I want the header to appear above the content, in a way I can see it through a transparent color, like a glass. Can I achieve such thing?

  • @raihanuddin1167
    @raihanuddin1167 3 года назад

    When I click 3 desh icon then left side show my component but my home component up thake mobile time then I don’t show properly how can solved it

  • @_BL4CKGaming
    @_BL4CKGaming 2 года назад

    still can do that, combine drawer and stack navigation in latest version?

  • @johncerpa3782
    @johncerpa3782 4 года назад

    Welcome !!

  • @swapnilsagar3805
    @swapnilsagar3805 3 года назад

    If anyone has successfully implemented the project till here, can you provide your GitHub/code link.
    My drawer closes itself as soon as I leave the screen and the hamburger is also not working.

  • @khayryazzez
    @khayryazzez 4 года назад

    how to change the color of the drawer background

  • @thanhat5907
    @thanhat5907 4 года назад

    The good tutorial but I have a problem. I have a mistake about auto alignItem center header "GameZone" so icon menu can't be on the left :(

    • @thanhat447
      @thanhat447 4 года назад

      I met a problem too. My title 'GameZone' on the left with a menu icon.

  • @100sainadh
    @100sainadh 4 года назад +1

    In On Press hamburger it is not working and The alignment of header text not in the center.Please help me

    • @uicubes
      @uicubes 4 года назад +2

      the alignment doesn't work with me

    • @100sainadh
      @100sainadh 4 года назад

      @@uicubes headerText:{
      fontWeight:'bold',
      fontSize:20,
      color:'#333',
      letterSpacing:1,
      marginLeft:60
      }
      try with this .Atleast u may get somewhat better result.Keep MarginLeft:60

  • @abitcompanyco2873
    @abitcompanyco2873 4 года назад

    The same happens to me that Asad Abdul. Please help me, how do I fix them?
    Thanks

  • @paristar3079
    @paristar3079 4 года назад +1

    Where do you live in UK?

  • @wendybarrera3348
    @wendybarrera3348 4 года назад

    how can i change the color of the header?

  • @gokulkannan7883
    @gokulkannan7883 4 года назад

    I am getting [X] icon instead of humburger icon

  • @ahmedsarmoum2956
    @ahmedsarmoum2956 4 года назад

    Thanks

  • @shamir-imtiaz
    @shamir-imtiaz 4 года назад

    Hello sir. I want to learn android development. But which will be best for android: flutter or react native. I am very confused. Thank you for your suggestion.

    • @shamir-imtiaz
      @shamir-imtiaz 4 года назад

      @@okradze thank you sir.For your suggestion.

    • @ImranSheikh-kg4qd
      @ImranSheikh-kg4qd 4 года назад +1

      I know react previously .... I haven't find anything difficult still now.
      So if you absolutely new then try to find-out .
      if know react then react-native could be your choice.

    • @shamir-imtiaz
      @shamir-imtiaz 4 года назад

      @@ImranSheikh-kg4qd Thank you bro. I am trying to learn flutter for android development. Thank you.

  • @robinware77
    @robinware77 4 года назад

    reactnavigation.org/docs/headers/#setting-the-header-title here is an link to v5 just in case you want to documentation.

  • @paungster
    @paungster 6 месяцев назад

    👏

  • @mrt2938
    @mrt2938 4 года назад

    wont work with ios iphone notch though

  • @lglf77
    @lglf77 4 года назад

    V5?

  • @petartodorov9202
    @petartodorov9202 4 года назад

    JUNE 2020 FIX: (I didn't get any of the other suggestions to work for me). To align the menu icon and the title correctly, I simply dropped all styling on the header and added textAlign: 'center' to the headerText. Icon style should stay the same with absolute positioning but I added zIndex: 1, since otherwise it is left under the title and is untouchable :
    const styles = StyleSheet.create({
    header: {
    // width: Dimensions.get('screen').width,
    // height: '100%',
    // flexDirection: 'row',
    // alignItems: 'center',
    // justifyContent: 'center'
    },
    headerText: {
    fontWeight: 'bold',
    fontSize: 20,
    color: '#333',
    letterSpacing: 1,
    textAlign: 'center'
    },
    icon: {
    position: 'absolute',
    left: 16
    ,
    zIndex: 1
    }
    });

  • @NimaEsfahani
    @NimaEsfahani 2 года назад

    🙂🙂

  • @alanfans6681
    @alanfans6681 4 года назад

    谢谢

  • @smartize
    @smartize 3 года назад

    here is how to pass navigation prop to custom Header in React Navigation v5
    export default Navigator = (props) => {
    const { navigation } = props;
    return (


    }} />


    )
    }

  • @raihanuddin1167
    @raihanuddin1167 3 года назад

    Any one plz help me

  • @skgamerff2073
    @skgamerff2073 2 года назад

    Gu ohg

  • @timzeynalov3537
    @timzeynalov3537 3 года назад +1

    this fucking navigation does not work