localdata01
localdata01

Reputation: 657

React navigation props to header

If anyone create a room then he get redirect to the room and I pass params to it like this:

useEffect(() => {
    createdGroup === true ? navigation.navigate('Room', { roomIdent }) : null;
}, [createdGroup]);

But I custom a header. How I pass the params to the header?

Stack.js

...
      <Stack.Screen 
        name="Room" 
        component={Room}
        options={
            {
            headerTitle: () => <HeaderRoom />,
            headerLeft: null,
            headerStyle: {
              elevation: 0,
              borderBottomWidth: 0
            }
          }
        }
        />

Upvotes: 2

Views: 1314

Answers (2)

Kartikey
Kartikey

Reputation: 4992

Working Example here

Make your Stack.Screen for Room like this

<Stack.Screen
      name="Room"
      component={Room}
      options={({ route }) => ({
        headerTitle: () => <HeaderRoom Title={route.params.name} />,
        headerLeft: null,
        headerStyle: {
          elevation: 0,
          borderBottomWidth: 0,
        },
      })}
   />

Then in your HeaderRoom.js use it like this

import React from 'react';

import { Text } from 'react-native';

function HeaderRoom(props) {
  console.log(props.Title) // it will log that custom title
  return (
    <Text style={{ fontWeight: 'bold', fontSize: 20 }}>{props.Title}</Text>
  );
}

export default HeaderRoom;

Upvotes: 3

LastPass876
LastPass876

Reputation: 1

You need Lifting State Up. Look at this example with official React Documentation

class Calculator extends React.Component {
  constructor(props) {
    super(props);
    this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
    this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
    this.state = {temperature: '', scale: 'c'};  }

  handleCelsiusChange(temperature) {
    this.setState({scale: 'c', temperature});  }

  handleFahrenheitChange(temperature) {
    this.setState({scale: 'f', temperature});  }

  render() {
    const scale = this.state.scale;    const temperature = this.state.temperature;    const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;    const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
    return (
      <div>
        <TemperatureInput
          scale="c"
          temperature={celsius}          onTemperatureChange={this.handleCelsiusChange} />        <TemperatureInput
          scale="f"
          temperature={fahrenheit}          onTemperatureChange={this.handleFahrenheitChange} />        <BoilingVerdict
          celsius={parseFloat(celsius)} />      </div>
    );
  }
}

You can read more here: https://reactjs.org/docs/lifting-state-up.html

Upvotes: 0

Related Questions