1

I want to set the title of my screen to a specific value when I click a button on another screen.

So what I want to achieve at the end is the following. From my HomeScreen I get to a ChangeScreen where I can save the user profile. Now in that screen when the user submits its name, that name should be shown in the header title for instance.

I still miss the part where the data is transferred from ChangeScreen to App.js.

I already found very similar question but I was not able to apply those solutions:

Find my Snack here.

App.js

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';

import Home from './components/HomeScreen';
import Change from './components/ChangeScreen';
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
const Stacks = createStackNavigator();
 
class App extends React.Component {
  state = {
    newtitle : 'foo'
  }
  
  handleLoginnameChange = newtitle => {
    this.setState({ newtitle })
  }

  render() {
    return (
      <NavigationContainer>
        <Stacks.Navigator
          headerMode="screen"
          screenOptions={{
            //headerTitle: this.state.newtitle,
            //headerTitle: props => <Change {...props} />
          }}
        >
        <Stacks.Screen
          name="home"
          component={Home}
          options={
            { 
              title: 'Home'
            }
          }
        />
        <Stacks.Screen
          name="change"
          component={Change}
          options={
            { 
              title: 'Change' 
            }
          }
        />
        </Stacks.Navigator>
      </NavigationContainer>
    );
  }
}

export default (App);

HomeScreen.js

import React from 'react'
import { Button, Text, View } from 'react-native';

class HomeScreen extends React.Component {
  render() {
    return (
      <View> 
        <Text>Hi, this is a test of change the title from another component</Text>
        <Button title='To the Change title screen' onPress={() => this.props.navigation.navigate('change')}>Change</Button>
      </View>
    ); 
  }
}
export default HomeScreen;

ChangeScreen.js

import React from 'react'
import { Button, Text, View } from 'react-native';

class ChangeScreen extends React.Component {
  render() {
    return (
      <View>
        <Text>Clicking this button will change the Header title to "TEST" and going back to Home.</Text>
        <Button title='Save' onPress={() => this.props.navigation.navigate('home')}>Home</Button>
    // on that button I need the setState or something similar like a onChange or onClick handler
      </View>
    );
  }
}
export default ChangeScreen;
kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • 1
    I think the best way is to use some state management tool. React provides a "Context" provider by default or you could use some external library like Redux. Changing state between siblings directly is kind of bad practice. – Mihail Feraru Aug 02 '20 at 20:03
  • 1
    Give [this](https://reactnavigation.org/docs/1.x/params/) a read. All you need is to send the Name you want to set as title in header as a parameter when navigating back to your HomeScreen and if when rendering the component you need to check if you have that parameter if yes then change to `title` to that. – Rupesh Chaudhari Aug 02 '20 at 20:17
  • Ok @RupeshChaudhari but isn't this a security issue? I mean this can easily be changed when I enable deeplinking. So if someone navigates to e.g. /home?newtitle=anything the system is kind of hacked, how to be save against something like that? – kwoxer Aug 03 '20 at 05:49
  • And @MihailFeraru I don't really want to use another Tool for such a simple task if I can avoid it. But maybe for security reason I should have a look on it? – kwoxer Aug 03 '20 at 10:03
  • 1
    If you are focusing on security then you should probably use any external state management tool like a `Context` Provider. – Rupesh Chaudhari Aug 03 '20 at 16:48
  • 1
    Thanks @RupeshChaudhari the https://reactjs.org/docs/context.html looks really for what I'm searching for. But I'm not able to apply it for me. They never show how to set the username. I simply need an example with 2 screens. One is the home, the other the ProfileSettings with name field and a save button. Now setting the name is changing the header title. Didn't think that its so complicated. Maybe that's not the best way and I should better create the user and write the id into a localStorage and then ask the database for the name of that id? WDYT? – kwoxer Aug 04 '20 at 05:27

0 Answers0