Sending Dynamically Changing Parameters from Functional Component to Navigation Options

Merve Dilek
Sep 22, 2020

--

In functional component’s navigation settings, we cannot take dynamically changing parameters directly from navigation data.

Suppose a functional component has a header button which takes information entered by user. In order to get the latest information given by the user, by using setParams() function, parameters can be passed to navigation options when user enters these values.

import React,{useState, useEffect, useCallback} from “react”;
import {TextInput} from “react-native”;
const foo = props => { const [name, setName]=useState(“”);
const handlerFunc=useCallback(()=>{
//when header button is pressed
},[name]) //never be recreated to avoid infinite loop
useEffect(()=> {props.navigation.setParams({handler:handlerFunc})},[handlerFunc]) return(
<View>
<TextInput value={name} onChangeText {(name)=>setName(name)}/>
</View>

In order to get the parameter from the component part use getParam()

foo.navigationOptions=navigationData=>{const handler=navData.navigation.getParam(“handler”);return {   headerRight:(<HeaderButtons 
HeaderButtonComponent= {YourHeaderButtonComponent}>
<Item
title=”save"
onPress={handler}
/>
</HeaderButtons>)}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Merve Dilek
Merve Dilek

Written by Merve Dilek

Computer Engineer / Bilkent University-2008

No responses yet

Write a response