Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

React native stackNavigator call function from header

Hi everyone I'm using stacknavigator. I just got 2 pages and everypage has functions. In the mainpage I'm fetching data. In mainpage header options I put touchableopacity in right on header, when onpress navigates second page.

First I gotta give the fetched data as navigation param but they'r in different file

Secondly I gotta call save function from header button which is in secondpage

navigation and options

const HomeStack = createStackNavigator()
const navOptFirstPage = ({ route, navigation }) => {

  return {
    headerTitleStyle: { alignSelf: 'center' },
    title: "title",
    headerTintColor: '#fff',
    headerStyle: {
      
      shadowColor: 'transparent',
    },

    headerLeft: () => (
      <Box mt={15} ml={10}>
        <TouchableOpacity >
          <Text color={'#FFFFFF'}>Hi</Text>
        </TouchableOpacity>
      </Box>
    ),

    headerRight: () => (
      <Box m={10}>
        <TouchableOpacity onPress={() => navigation.navigate('SecondPage')}>
          <Text color={'#FFFFFF'}>Edit with route param</Text>
        </TouchableOpacity>
      </Box>
    ),
  };
};
const navOptSecondPage = ({ route, navigation }) => {
  return {
    headerTitleStyle: { alignSelf: 'center' },
    title: "title",
    headerTintColor: '#fff',
    headerStyle: {
     
      shadowColor: 'transparent',
    },

   

    headerRight: () => (
      <Box m={10}>
        <TouchableOpacity >
          <Text color={'#FFFFFF'}>Save</Text>
        </TouchableOpacity>
      </Box>
    ),
  };
};
function HomeStackS() {

  return (
    <HomeStack.Navigator>
      <HomeStack.Screen
        name="FirstPage"
        component={FirstPage}
        options={navOptFirstPage}
        

      />
      <HomeStack.Screen
        name="SecondPage"
        component={SecondPage}
        options={navOptSecondPage}

      />
    </HomeStack.Navigator>
  )
}

first page componentDidmount

componentDidMount() {   
    const url = "https://aaaaXXXX1.herokuapp.com/getData";
    fetch(url).then((response) => response.json())
    .then((responseJson) => {
        this.setState({
            //this is the data i gotta apss it to other screen
        dataSource: responseJson.data,        
        })
    })
    .catch((error) => {
        console.log(error);
    });
  }

second page funcion i gotta call it from header

componentDidMount() {
    //sets the data
    this.setState(
        {
          dataSource: this.props.route.params.data,        
        },
    this.props.navigation.setParams({ functionCallFromHeader: this.functionCallFromHeader});
  }
  functionCallFromHeader = () => {
    alert("Awesome");
  }

Look like this enter image description here

question from:https://stackoverflow.com/questions/65860158/react-native-stacknavigator-call-function-from-header

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...