Notes

Passing params to React Navigation Header in Functional Components

Edit on GitHub

React Native & Expo

You set the param first with navigation.setParams. If it’s gonna change often, you’ll probably set it in useEffect()

 1import React, { useEffect, useState } from 'react'
 2import { useNavigation } from 'react-navigation-hooks'
 3
 4export function Conversations() {
 5  const navigation = useNavigation()
 6
 7  const [showArchived, setShowArchived] = useState(false)
 8  const [search, setSearch] = useState('')
 9  const [isSearching, setIsSearching] = useState(false)
10
11  useEffect(() => {
12    navigation.setParams({ showArchived })
13    navigation.setParams({ setShowArchived })
14    navigation.setParams({ isSearching })
15    navigation.setParams({ setIsSearching })
16    navigation.setParams({ search })
17    navigation.setParams({ setSearch })
18  }, [showArchived, isSearching, search])
19
20return ()
21}
22
23Conversations.navigationOptions = ({
24  navigation,
25}: {
26  navigation: NavigationStackProp<NavigationRoute>,
27}): NavigationStackOptions => {
28  const showArchived = navigation.getParam('showArchived', false)
29  const setShowArchived = navigation.getParam('setShowArchived', false)
30  const isSearching = navigation.getParam('isSearching', false)
31  const setIsSearching = navigation.getParam('setIsSearching', false)
32  const search = navigation.getParam('search', '')
33  const setSearch = navigation.getParam('setSearch', '')
34
35  return {}
36}