React Native Tutorial #22 - Passing Data Between Screens

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ต.ค. 2024
  • 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/iam...
    🐱‍💻 🐱‍💻 Other Related Courses:
    + Complete React Tutorial - • Complete React Tutoria...
    + React Hooks & Context Tutorial - • React Context & Hooks ...
    + Modern JavaScript Tutorial - • Modern JavaScript Tuto...

ความคิดเห็น • 126

  • @nikulasoskarsson5472
    @nikulasoskarsson5472 4 ปีที่แล้ว +145

    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' },
    ]);

  • @oguzhanolguncu
    @oguzhanolguncu 4 ปีที่แล้ว +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 4 ปีที่แล้ว

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

  • @uxninjaa
    @uxninjaa 4 ปีที่แล้ว +9

    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 3 ปีที่แล้ว

      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 3 ปีที่แล้ว +1

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

  • @muhdibee
    @muhdibee 2 ปีที่แล้ว +1

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

  • @tanujnamdeo
    @tanujnamdeo 4 ปีที่แล้ว +6

    Please do a series on React navigation 5.x.

  • @andresgonzalezmartinez5691
    @andresgonzalezmartinez5691 4 ปีที่แล้ว +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 2 ปีที่แล้ว

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

    • @ojitelikenechukwu3895
      @ojitelikenechukwu3895 2 ปีที่แล้ว

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

    • @erbudak
      @erbudak 2 ปีที่แล้ว +2

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

  • @Jonnymad1245
    @Jonnymad1245 3 ปีที่แล้ว +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

  • @arwahsapi
    @arwahsapi 4 ปีที่แล้ว +1

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

  • @SudhanshuDubey-v9f
    @SudhanshuDubey-v9f 9 หลายเดือนก่อน +1

    *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)}
    );
    }

  • @paristar3079
    @paristar3079 4 ปีที่แล้ว +10

    Smash the like button. He really deserves.

    • @NetNinja
      @NetNinja  4 ปีที่แล้ว +2

      Thanks Pari :)

  • @JuanDeSouza7
    @JuanDeSouza7 4 ปีที่แล้ว +9

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

    • @Athuldk
      @Athuldk 4 ปีที่แล้ว +3

      What change to be done oh home.js

    • @BukkyOdunsi
      @BukkyOdunsi 6 หลายเดือนก่อน

      Thank you

  • @abeprangishvili
    @abeprangishvili 4 ปีที่แล้ว

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

  • @learnedcurve3279
    @learnedcurve3279 4 ปีที่แล้ว +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 3 ปีที่แล้ว +2

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

    • @badbrother_2023
      @badbrother_2023 2 ปีที่แล้ว

      Thank you so much but no need to destructure item.

    • @ojitelikenechukwu3895
      @ojitelikenechukwu3895 2 ปีที่แล้ว

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

  • @juliocesarmontanha6901
    @juliocesarmontanha6901 3 ปีที่แล้ว

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

  • @siddhantsonar7362
    @siddhantsonar7362 4 ปีที่แล้ว +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.

  • @abdullahfarooqui2479
    @abdullahfarooqui2479 2 ปีที่แล้ว +1

    this doesnt work anymore with react navigation v6

  • @terry_swd
    @terry_swd 2 ปีที่แล้ว +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}

    )
    }

  • @IneMalnStock
    @IneMalnStock 4 ปีที่แล้ว +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}
    );
    }

  • @lukeinderwick5951
    @lukeinderwick5951 4 ปีที่แล้ว +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".

  • @TheNerdyDev
    @TheNerdyDev 4 ปีที่แล้ว

    Superb content as always ❤

  • @AzoltoFire
    @AzoltoFire 4 ปีที่แล้ว

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

  • @mayursarode1
    @mayursarode1 3 ปีที่แล้ว

    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??

  • @HolyQuran0992
    @HolyQuran0992 ปีที่แล้ว

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

  • @qualityuz2055
    @qualityuz2055 4 ปีที่แล้ว +2

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

  • @BesaAlmahdy
    @BesaAlmahdy 4 ปีที่แล้ว

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

  • @heisenberg6442
    @heisenberg6442 10 หลายเดือนก่อน

    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

  • @rushtothemax76
    @rushtothemax76 4 ปีที่แล้ว +2

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

  • @donhathong112k
    @donhathong112k ปีที่แล้ว +1

    omg thank you very much, i love u so much

    • @NetNinja
      @NetNinja  ปีที่แล้ว

      No problem Raphael, hope it was helpful! 😊

  • @VinodKumar-sd6zk
    @VinodKumar-sd6zk 3 ปีที่แล้ว +2

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

    • @Karinnederland
      @Karinnederland 3 ปีที่แล้ว

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

  • @_wise_one
    @_wise_one 4 ปีที่แล้ว +2

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

  • @SaintPepsiSanCoca
    @SaintPepsiSanCoca 4 ปีที่แล้ว +2

    this is mind blowing

  • @amaadali1927
    @amaadali1927 4 ปีที่แล้ว +1

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

  • @sudonitin
    @sudonitin 4 ปีที่แล้ว +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

  • @איתישכנר-ו8ח
    @איתישכנר-ו8ח 4 ปีที่แล้ว +1

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

    • @Athuldk
      @Athuldk 4 ปีที่แล้ว

      What change to be done oh home.js

  • @sunilkumawat4399
    @sunilkumawat4399 2 ปีที่แล้ว

    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') }
    )
    }

  • @fayasbacker123
    @fayasbacker123 4 ปีที่แล้ว +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?

  • @Verum_Rex
    @Verum_Rex ปีที่แล้ว

    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?

  • @sajidjabbar5650
    @sajidjabbar5650 4 ปีที่แล้ว +1

    Thank you so much , really appreciate your work

  • @Kaybelo_
    @Kaybelo_ 2 ปีที่แล้ว +1

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

  • @jogre900
    @jogre900 3 ปีที่แล้ว

    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!!

  • @alirezaesbati6530
    @alirezaesbati6530 4 ปีที่แล้ว +1

    the best video tutorial i have ever watched !!

  • @aymangafoor5792
    @aymangafoor5792 4 ปีที่แล้ว

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

  • @shubhanshusahuu
    @shubhanshusahuu 3 ปีที่แล้ว

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

  • @mrarslanfaaris1875
    @mrarslanfaaris1875 2 ปีที่แล้ว

    facing error get.Param is not a function?

  • @FirasKarboul
    @FirasKarboul 4 ปีที่แล้ว

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

  • @Mr92Metallica
    @Mr92Metallica 4 ปีที่แล้ว

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

  • @gabrielmarcano2247
    @gabrielmarcano2247 2 ปีที่แล้ว +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 }

    )
    }

  • @Boga_adventures
    @Boga_adventures 4 ปีที่แล้ว

    Awesome

  • @kazimali9727
    @kazimali9727 3 ปีที่แล้ว

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

  • @Dewmith
    @Dewmith ปีที่แล้ว

    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 ปีที่แล้ว +1

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

      );
      }

  • @theshubhagrwl
    @theshubhagrwl 4 ปีที่แล้ว

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

  • @mv9321
    @mv9321 3 ปีที่แล้ว

    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!

  • @kevinthomas3669
    @kevinthomas3669 3 ปีที่แล้ว

    Can you pass a function?

  • @akashkansyakar4648
    @akashkansyakar4648 3 ปีที่แล้ว

    How to call function using different parms

  • @Penham4n
    @Penham4n 3 ปีที่แล้ว

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

  • @Omerko
    @Omerko 4 ปีที่แล้ว

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

  • @yolandajoseribasbastidas8460
    @yolandajoseribasbastidas8460 3 ปีที่แล้ว

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

  • @thethinkend
    @thethinkend 3 ปีที่แล้ว

    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 3 ปีที่แล้ว

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

    • @challenges8010
      @challenges8010 3 ปีที่แล้ว +1

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

    • @bluberxx
      @bluberxx 3 ปีที่แล้ว +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 3 ปีที่แล้ว +1

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

      {title}


      )
      }

    • @bluberxx
      @bluberxx 3 ปีที่แล้ว +2

      I would be gratefull for your help

  • @Crosfake
    @Crosfake 4 ปีที่แล้ว

    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?

  • @tattarrrrattat
    @tattarrrrattat 4 ปีที่แล้ว

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

  • @yawningoutside4276
    @yawningoutside4276 4 ปีที่แล้ว

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

    • @NetNinja
      @NetNinja  4 ปีที่แล้ว +1

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

  • @manoj-k
    @manoj-k 2 ปีที่แล้ว

    🔥🔥🔥

  • @guymross
    @guymross 3 ปีที่แล้ว

    Thank you so much!

  • @ahmedyousif4782
    @ahmedyousif4782 3 ปีที่แล้ว

    you are awesome!

  • @anvuongnguyen5496
    @anvuongnguyen5496 2 ปีที่แล้ว

    amazing tutorial

  • @up-beat-music
    @up-beat-music 3 ปีที่แล้ว

    thank you

  • @rg-du4ou
    @rg-du4ou 4 ปีที่แล้ว

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

    • @B00Ginat0r
      @B00Ginat0r 2 ปีที่แล้ว

      Literally shown in the video

  • @Juanlopezalurralde
    @Juanlopezalurralde 4 ปีที่แล้ว

    So powerfull...

  • @jenzwren2434
    @jenzwren2434 4 ปีที่แล้ว

    😁😁😁😁

  • @gulsherkhan5206
    @gulsherkhan5206 4 ปีที่แล้ว

    wooooo 😍😍

  • @alihusain9033
    @alihusain9033 4 ปีที่แล้ว

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

    • @aamiramin6112
      @aamiramin6112 4 ปีที่แล้ว

      Same error :(

    •  4 ปีที่แล้ว +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 4 ปีที่แล้ว

      @ 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 4 ปีที่แล้ว

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

    • @Karinnederland
      @Karinnederland 3 ปีที่แล้ว

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

  • @lewislin7835
    @lewislin7835 4 ปีที่แล้ว

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