React Native Tutorial #22 - Passing Data Between Screens

Sdílet
Vložit
  • čas přidán 19. 12. 2019
  • Hey gang, in this React Native tutorial we'll see how to pass data between screens when navigating from one to another (in our case from Home to Review Details).
    ----------------------------------------
    🐱‍💻 🐱‍💻 Course Links:
    Course files - github.com/iamshaunjp/react-n...
    🐱‍💻 🐱‍💻 Other Related Courses:
    + Complete React Tutorial - • Complete React Tutoria...
    + React Hooks & Context Tutorial - • React Context & Hooks ...
    + Modern JavaScript Tutorial - • Modern JavaScript Tuto...

Komentáře • 126

  • @nikulasoskarsson5472
    @nikulasoskarsson5472 Před 4 lety +143

    const [reviews, setReviews] = useState([
    { title: 'Zelda, Breath of Fresh Air', rating: 5, body: 'lorem ipsum', key: '1' },
    { title: 'Gotta Catch Them All (again)', rating: 4, body: 'lorem ipsum', key: '2' },
    { title: 'Not So "Final" Fantasy', rating: 3, body: 'lorem ipsum', key: '3' },
    ]);

  • @rushtothemax76
    @rushtothemax76 Před 4 lety +2

    Everytime I have to learn something, one of your tutorials saves the day

  • @_wise_one
    @_wise_one Před 4 lety +2

    this is one of the best channel on youtube. keep it up

  • @sudonitin
    @sudonitin Před 4 lety +1

    Hey man, Thank you for this tutorial. Helped a lot..!
    I request all viewers to support the video that helps you in any way. There are more viewers who might need to see these videos.
    Best Regards..!
    Cheers to all devs

  • @alirezaesbati6530
    @alirezaesbati6530 Před 4 lety +1

    the best video tutorial i have ever watched !!

  • @amaadali1927
    @amaadali1927 Před 4 lety +1

    Really good work.. And the way of explanation.. Amazing

  • @paristar3079
    @paristar3079 Před 4 lety +10

    Smash the like button. He really deserves.

  • @sajidjabbar5650
    @sajidjabbar5650 Před 4 lety +1

    Thank you so much , really appreciate your work

  • @yolandajoseribasbastidas8460

    Thank you so much for these videos, help me a lot!

  • @AzoltoFire
    @AzoltoFire Před 4 lety

    Really good videos, this is exactly what i wanted,many thanks

  • @muhdibee
    @muhdibee Před 2 lety +1

    Nice, This helped me in my project, thanks alot.

  • @mv9321
    @mv9321 Před 3 lety

    This method still works with Drawer Navigation, or atleast that was the only way I found to pass params between screens. Using route.params would be "undefined" and crash the app. So thank you for this video!

  • @juliocesarmontanha6901

    Great Job. My problem is resolved. Thanks!!!

  • @TheNerdyDev
    @TheNerdyDev Před 4 lety

    Superb content as always ❤

  • @abeprangishvili
    @abeprangishvili Před 4 lety

    traversy media and net ninja you are the best !!!!!!!! fantastic men !

  • @SaintPepsiSanCoca
    @SaintPepsiSanCoca Před 4 lety +2

    this is mind blowing

  • @tanujnamdeo
    @tanujnamdeo Před 3 lety +6

    Please do a series on React navigation 5.x.

  • @Crosfake
    @Crosfake Před 4 lety

    Shaun Hello! Your content is awesome =) keep up a good job! Just a lil question. Is it possible in near future u will make an updated Angular content?

  • @guymross
    @guymross Před 3 lety

    Thank you so much!

  • @donhathong112k
    @donhathong112k Před rokem +1

    omg thank you very much, i love u so much

    • @NetNinja
      @NetNinja  Před rokem

      No problem Raphael, hope it was helpful! 😊

  • @mayursarode1
    @mayursarode1 Před 3 lety

    Great tutorial, I have a query, can we update route.params which we had received from screen 1 and then the updated (using ) one send to next screen??

  • @anvuongnguyen5496
    @anvuongnguyen5496 Před 2 lety

    amazing tutorial

  • @BesaAlmahdy
    @BesaAlmahdy Před 4 lety

    Great videos, I will translate this tut to my language later if you allowed that, Thanks man 😊

  • @ahmedyousif4782
    @ahmedyousif4782 Před 3 lety

    you are awesome!

  • @siddhantsonar7362
    @siddhantsonar7362 Před 4 lety +2

    Great video,
    Can you make a video on react native - redux - and passing data between screen/components
    basically, i am stuck in my application where i have switch navigator at the beginning that is login page, after that, i have a bottom tab navigator which has 3 tabs in it and the problem is i want the username from the login screen to be passed to all 3 of the tabs in the bottom tab navigator. If you can help me out, it would be great.

  • @learnedcurve3279
    @learnedcurve3279 Před 3 lety +10

    For those who are working with a more updated navigation
    export default function ReviewDetails({ route, navigation }) {
    const { item } = route.params;
    return (
    {item.title}
    );
    }

    • @TechFriendly
      @TechFriendly Před 3 lety +2

      route.params itself returns the item. No need to deconstruct it.

    • @badbrother_2023
      @badbrother_2023 Před 2 lety

      Thank you so much but no need to destructure item.

    • @ojitelikenechukwu3895
      @ojitelikenechukwu3895 Před 2 lety

      @bad brother_20 how did you go about yours, I'm stuck, using v6

  • @Boga_adventures
    @Boga_adventures Před 4 lety

    Awesome

  • @uxninjaa
    @uxninjaa Před 3 lety +8

    Little update for React Navigate 5:
    export default function ReviewDetails({ route, navigation }) {
    const { title, rating, body } = route.params;
    return(

    {JSON.stringify(title)}

    {JSON.stringify(rating)}

    {JSON.stringify(body)}

    )
    }

    • @Karinnederland
      @Karinnederland Před 3 lety

      you should pin it :) for me it says name is undefined because in my app one of the properties is name, why could this be? the rest of the code is exactly as yours and name is one of the properties :(

    • @joeharris4737
      @joeharris4737 Před 3 lety +1

      I don't see why you need JSON.stringify , it works fine without it

  • @arwahsapi
    @arwahsapi Před 4 lety +1

    Your voice is really like Dr. James Grime from Numberphile channel.

  • @JuanDeSouza7
    @JuanDeSouza7 Před 4 lety +9

    There is no getParam method in "navigation" object anymore. Now you have to use props.route.params['propertyname']

  • @up-beat-music
    @up-beat-music Před 3 lety

    thank you

  • @terry_swd
    @terry_swd Před 2 lety +1

    January 2022: it seems like to receive the params you now need to destructure "route" in addition to "navigation" in the ReviewDetails component, and then get the params by using something like this:
    export default function ReviewDetails({ route, navigation }) {
    return (

    {route.params.item.title}

    )
    }
    or else use {item} as a constant with "route.params" and then just use "item.title" to pull out the value (someone else mentioned this in another comment):
    export default function ReviewDetails({ route, navigation }) {
    const { item } = route.params;
    return (

    {item.title}

    )
    }

  • @shubhanshusahuu
    @shubhanshusahuu Před 3 lety

    Thank you soo much
    But if i want to send whole array of object then what we need to do?

  • @Omerko
    @Omerko Před 4 lety

    More of a Angular Guy, but this series is just great..

  • @JoaoPereira-tx9ib
    @JoaoPereira-tx9ib Před 3 lety

    Hey, when you want to do a flatlist, followed by another flatlist after chosing a item, how do you do that?

  • @manoj-k
    @manoj-k Před 2 lety

    🔥🔥🔥

  • @user-kn6cg2gq1z
    @user-kn6cg2gq1z Před 4 lety +1

    For anyone who is watching this video after the v5 upgrade, use props.route.params.title etc. works the same

    • @Athuldk
      @Athuldk Před 3 lety

      What change to be done oh home.js

  • @tattarrrrattat
    @tattarrrrattat Před 4 lety

    I didn't know Owen Jones was a web developer also! :D

  • @gulsherkhan5206
    @gulsherkhan5206 Před 4 lety

    wooooo 😍😍

  • @Juanlopezalurralde
    @Juanlopezalurralde Před 4 lety

    So powerfull...

  • @lukeinderwick5951
    @lukeinderwick5951 Před 4 lety +3

    If anyone gets an error "Warning: Failed child context type: Invalid child context `virtualizedCell.cellKey` of type `number` supplied to `CellRenderer`, expected `string`."
    Change your item key values 1, 2, 3 to strings. So "1", "2", "3".

  • @Jonnymad1245
    @Jonnymad1245 Před 3 lety +2

    I am using "@react-navigation/native": "^5.9.4" and "react-navigation-stack": "^2.10.4" and it works fine. route.params doesnt work

  • @heisenberg6442
    @heisenberg6442 Před 7 měsíci

    Will this method work when both the screens are .tsx format, If not what method could I use for for the passing of data between 2 tsx files

  • @fayasbacker123
    @fayasbacker123 Před 4 lety +2

    Hi,
    I am using react navigation 4.3.9. I have a setup where I have to pass data between a page containing react class component to a page containing functional component. I am using stack navigator for navigation. I am able to navigate to the second page using the command: that.props.navigation.navigate("Home", {title: 'text'}) but in the second screen when i try to access the passed data using the codes in the video I am getting undefined value for title. Can u please help me with this issue?

  • @andresgonzalezmartinez5691
    @andresgonzalezmartinez5691 Před 3 lety +18

    import React from 'react'
    import {View, Text, Button} from 'react-native'
    import {globalStyles} from '../styles/global'
    export default function ReviewDetails({route, navigation}){
    const { title } = route.params;
    const { body } = route.params;
    const { rating } = route.params;
    const pressHandler = () => {
    navigation.goBack()
    }

    return(

    {JSON.stringify(title)}
    {JSON.stringify(body)}
    {JSON.stringify(rating)}


    )
    }

    • @badbrother_2023
      @badbrother_2023 Před 2 lety

      Hey it worked but I have an error on an image. You got any solution to that.

    • @ojitelikenechukwu3895
      @ojitelikenechukwu3895 Před 2 lety

      Same here, you are using version 6 right?
      Please any help is needed, I have been stuck

    • @erbudak
      @erbudak Před 2 lety +2

      @@badbrother_2023 just write {route.params.title}, no need to use JSON stringify.

  • @IneMalnStock
    @IneMalnStock Před 4 lety +2

    React navigation 5.
    I am using the code formatter: prettier.io/
    home.js
    ___________________________________________
    import {
    StyleSheet,
    Text,
    View,
    FlatList,
    TouchableOpacity,
    } from "react-native";
    import React, { useState } from "react";
    import { globalStyles } from "../styles/global";
    export default function Home({ navigation }) {
    const [reviews, setReviews] = useState([
    {
    title: "Zelda, Breath of Fresh Air",
    rating: 5,
    body: "lorem ipsum",
    key: "1",
    },
    {
    title: "Gotta Catch Them All (again)",
    rating: 4,
    body: "lorem ipsum",
    key: "2",
    },
    {
    title: 'Not So "Final" Fantasy',
    rating: 3,
    body: "lorem ipsum",
    key: "3",
    },
    ]);
    return (
    (
    navigation.navigate("ReviewDetails", item)}
    >
    {item.title}
    )}
    />
    );
    }
    reviewDetails.js
    ___________________________________________
    import {StyleSheet, Text, View, Button} from 'react-native';
    import { globalStyles } from '../styles/global';
    import React from 'react';
    export default function ReviewDetails({ route, navigation }) {
    const { title, body, rating } = route.params;
    return (
    {title}
    {body}
    {rating}
    );
    }

  • @FirasKarboul
    @FirasKarboul Před 4 lety

    can i use navigate.getParam when i pass data between screens in drawer? cuz it doesn't work for me...!

  • @jogre900
    @jogre900 Před 3 lety

    How can i pass just an id to do some fetch from an api every time i navigate to detailView? My id from route.params dosent update. So i make the.call to same endpoint every time, help!!

  • @Verum_Rex
    @Verum_Rex Před rokem

    I am experiencing slow performance issue when I pass large amounts of data. How could I still keep the amount of data transfer, but improve the slow performance?

  • @Mr92Metallica
    @Mr92Metallica Před 4 lety

    I want to pass data through a drawer but this method doesn't work (using navigation.getParam() ) ! What should I do ?

  • @Kaybelo_
    @Kaybelo_ Před 2 lety +1

    Is there a way to pass those values using react navigation v6.x?

  • @kazimali9727
    @kazimali9727 Před 2 lety

    how can we do same thing if we want to fetch data from database.??

  • @VinodKumar-sd6zk
    @VinodKumar-sd6zk Před 3 lety +2

    After clicking an item it shows an exception like this:
    undefined is not an object (evaluating navigation.navigate)

    • @Karinnederland
      @Karinnederland Před 3 lety

      I have the same problem, how did you resolve it? please let me know:)

  • @jenzwren2434
    @jenzwren2434 Před 4 lety

    😁😁😁😁

  • @akashkansyakar4648
    @akashkansyakar4648 Před 3 lety

    How to call function using different parms

  • @abdullahfarooqui2479
    @abdullahfarooqui2479 Před 2 lety +1

    this doesnt work anymore with react navigation v6

  • @Dewmith
    @Dewmith Před rokem

    Hi, Anyone who is struggling, upwards React Native Navigation v5, getParams is not available in navigation prop, there's a new prop named 'route' and u can get the data by calling the route.params.. Cheers

    • @ramziosta
      @ramziosta Před rokem +1

      return (
      Review Details
      {route.params.title}
      {route.params.rating}
      {route.params.body}

      );
      }

  • @mrarslanfaaris1875
    @mrarslanfaaris1875 Před 2 lety

    facing error get.Param is not a function?

  • @rg-du4ou
    @rg-du4ou Před 4 lety

    hi, How to pass the data between screens using Button ?

  • @user-qh1qe8of5b
    @user-qh1qe8of5b Před 6 měsíci

    *Update*
    getParams is not working anymore. Params will get in another variable "route".
    export default function ReviewDetails({ route, navigation }) {
    const item = route.params;
    return (
    {JSON.stringify(item.title)}
    {JSON.stringify(item.body)}
    {JSON.stringify(item.rating)}
    );
    }

  • @thethinkend
    @thethinkend Před 3 lety

    22/07/2021: Read...
    export default function Details ({route, navigation}){
    const { titleProduct, priceProduct, descriptionProduct, detailsProduct } = route.params;
    Inside the braces { } after 'const' you have to use the same names that you used in Home.js inside the useState object.
    return(

    {titleProduct}
    {priceProduct}
    {descriptionProduct}
    {detailsProduct}

    )
    }

    • @bluberxx
      @bluberxx Před 2 lety

      Did you changed something in home.js? When Im trying to use it I have an error "title is undefined"

    • @challenges8010
      @challenges8010 Před 2 lety +1

      @@bluberxx do u have a repo? i can check for u, new versions of react native have a lot changes

    • @bluberxx
      @bluberxx Před 2 lety +1

      @@challenges8010 unfortunately if I add link to my repo, my comments are deleted
      . Here is my home.js:
      function Home({ navigation }) {
      const [reviews, setReviews] = useState([
      { title: "Zelda", rating: 1, body: "lorem ipsum", key: 1 },
      { title: "Cruella", rating: 1, body: "lorem ipsum", key: 2 },
      { title: "Voldemort", rating: 1, body: "lorem ipsum", key: 3 },
      ]);
      return (

      (

      navigation.navigate({
      routeName: "reviewDetails",
      params: { item }
      })
      }
      >
      {item.title}

      )}
      />

      );
      }
      function Home({ navigation }) {
      const [reviews, setReviews] = useState([
      { title: "Zelda", rating: 1, body: "lorem ipsum", key: 1 },
      { title: "Cruella", rating: 1, body: "lorem ipsum", key: 2 },
      { title: "Voldemort", rating: 1, body: "lorem ipsum", key: 3 },
      ]);
      return (

      (

      navigation.navigate({
      routeName: "reviewDetails",
      params: { item }
      })
      }
      >
      {item.title}

      )}
      />

      );
      }

    • @bluberxx
      @bluberxx Před 2 lety +1

      and my reviewsDetails.js
      export default function Details ({route, navigation}){
      const { title } = route.params;
      return(

      {title}


      )
      }

    • @bluberxx
      @bluberxx Před 2 lety +2

      I would be gratefull for your help

  • @aymangafoor5792
    @aymangafoor5792 Před 4 lety

    How can I pass lat to a screen from below json results
    geometry:{location:{lat:"11.1",lng:"72.11"}}
    Plss anybody 🙏😩😩

  • @MasterDesignss
    @MasterDesignss Před rokem

    How to pass Images between screens using params route. react native

  • @sunilkumawat4399
    @sunilkumawat4399 Před 2 lety

    import React from
    import { StyleSheet, View, Text, Button } from 'react-native';
    react';
    import { globalStyles } from '../styles/global';
    export default function ReviewDetails({ navigation }) {
    return (

    { navigation.getParam('title') }
    { navigation.getParam('body') }
    { navigation.getParam('rating') }
    )
    }

  • @qualityuz2055
    @qualityuz2055 Před 3 lety +2

    Why get.Param is not working? Showing error like "get.Param is not a function" and get.Param is undefined? thanks before hand

  • @alihusain9033
    @alihusain9033 Před 4 lety

    i have an error can't find variable navigate how can i solve it

    • @aamiramin6112
      @aamiramin6112 Před 4 lety

      Same error :(

    •  Před 4 lety +4

      They have updated the command. Check this out - reactnavigation.org/docs/upgrading-from-4.x/#navigate under "No more getParam". Import "route" instead, and change it to {route.params?.title ?? 'defaultValue'} and it will work. Good luck!

    • @Boga_adventures
      @Boga_adventures Před 4 lety

      @ thanks i had same error but i was able to get rid of it with your help. The next issue I'm having is the next screen doesn't display the titles it just displays the defaultValue. can you help me?

    • @Boga_adventures
      @Boga_adventures Před 4 lety

      @André Persson i just figured everything out ..solved my issues . thanks alot

    • @Karinnederland
      @Karinnederland Před 3 lety

      @ how and where do you import it? please let me know

  • @kevinthomas3669
    @kevinthomas3669 Před 2 lety

    Can you pass a function?

  • @theshubhagrwl
    @theshubhagrwl Před 4 lety

    If anyone is getting undefined error : reactnavigation.org/docs/params/

  • @lewislin7835
    @lewislin7835 Před 3 lety

    I am getting "navigation.getParam is not a function" error. How to fix it? Thanks

  • @yawningoutside4276
    @yawningoutside4276 Před 4 lety

    I don't see a link to your Patreon. Dude, just shut up and take my money!

    • @NetNinja
      @NetNinja  Před 4 lety +1

      Haha, thanks so much dude, it's on the Net Ninja CZcams homepage :)
      www.patreon.com/thenetninja

  • @gabrielmarcano2247
    @gabrielmarcano2247 Před 2 lety +1

    If anyone gets an error "TypeError: navigation.getParam is not a function.", destructure route into the component and use route.params.KEY instead
    e.g.
    export default function ReviewDetails({ route, navigation }) {
    return (

    { route.params.title }
    { route.params.body }
    { route.params.rating }

    )
    }

  • @oguzhanolguncu
    @oguzhanolguncu Před 4 lety +17

    const [reviews, setReviews] = useState([
    { title: 'Zelda, Breath of Fresh Air', rating: 5, body: 'lorem ipsum', key: 1 },
    { title: 'Gotta catch Them All(again)', rating: 4, body: 'lorem ipsum', key: 2 },
    { title: 'Not So "Final" Fantasy', rating: 3, body: 'lorem ipsum', key: 3 },
    ]);

    • @StarBattle08
      @StarBattle08 Před 4 lety

      The Key need to be a string, otherwise you'll get a warning. Or you can add this property to the Flastlist
      keyExtractor={(item) => item.key.toString()}