65983

How to pass data between react native screens?

Question:

Hey I am new to react native. So basically I have a stack navigator with two screen option: Screen 1 (Default) and Screen2. I already set up a button so that when pressed it will take me to Screen 2. So in Screen1 I have an array displayed as scrollview component. Now when i press the button to go to screen2 I want to pass some of the array values to that screen2 component. What is the best way to do it? Thank you :)

I am really new so my attempts are kinda dumb. I tried importing the Screen1 component and calling the array values via this.state but no access.

Answer1:

The easiest way to pass the data is when you are navigating. To do that it is better that you put your array in a state an then use the following method:

onPress=()=>{ this.props.navigation.navigate('Screen2', { yourArray: this.state.yourArray, }); } }

Then in the next screen(Screen2) you can find the array(your data) in the props.So in your constructor in scrren2 you can find the data here:

constructor(props) { super(props); var params = props.navigation.state.params.yourArray; }

Also to come back to your previous screen without touching your states you can use this code in your back button:

const { goBack } = this.props.navigation; goBack();

Recommend

  • Model Binding - Type in External Assembly
  • How to exclude path from precompilation for AspNetCompiler in csproj
  • Maven scanned from multiple locations type of warnings
  • Changing top level routes for multiple Angular apps served from single nginx server?
  • kotlin wrong nullability inference without any generics
  • Function which returns multiple values
  • Spark throws java.util.NoSuchElementException: key not found: 67
  • Python3: UnicodeEncodeError: 'ascii' codec can't encode character '\\xfc'
  • How to disable paging for JpaRepository in spring-data-rest
  • Can a Makefile execute code ONLY when an error has occurred?
  • Syntax error or access violation: 1059 Identifier name is too long
  • Script Element Between Head and Body. [HTML5] [closed]
  • seaborn heatmap get array of color codes values
  • OpenGL es 1.1 - android - does gl.glDeleteTextures free video memory?
  • Why do I get IPC delays on 20% busy machine
  • How to test Rate this app functionality before submitting to app store
  • Changing UITableViewCell's width constraint's constant value for each instance
  • How to properly create a RollingFileAppender with AppenderComponentBuilder in log4j 2
  • Generic Return Type Based on Class
  • Get all the values from excel file by using linqtoexcel
  • VC++ 6.0 access violation when run in debugger
  • How to use multi dimensional STL vector in MPI
  • How would I make a polygon based on the relative positions of markers in google maps?
  • Migrating MOSS 2007 from SQL 2000 to SQL 2005 [closed]
  • CSS style for container with transparent border images
  • Gitlab: copy project to other git lab repository
  • Is there a .NET method equivalent to NETWORKDAYS in Excel?
  • Can't hide status bar in AVPlayerViewController's portrait mode
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • VBScript InputBox and Help Files
  • AWS RDS Parameter Group not changing MySQL encoding
  • Year over Year Stats from a Crossfilter Dataset
  • Geokit in Ruby on Rails, problem with acts_as_mappable
  • Cloud Code: Creating a Parse.File from URL
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to get rgb from transparent pixel in js