React Native : how to change view on onPress event

Mohammad Siavashi

i have a button on my first rendered View ( the default View ) , i want to change the View when the user press that button .

i know how to use the onPress event but i dont know how should i change the whole View ? im creating another react.createClass which has the new render and other stuffs in it but i dont know how should i use it for the View to be Changed .

here is my first View ( the Main one ) ( by the way the application name is sess ):

var sess = React.createClass({
  render(){
    return(

      <View>
        <Button> change view </Button>  //onPress is gonna be here
      </View>
    );
  },
});

and i want the View to be changed to this :

var newView = React.createClass({
  render(){
    return(

      <View>
        <Text> the View is now changed </Text>
      </View>
    );
  },
});
Nader Dabit

You can do it like this:

var sess = React.createClass({

  getInitialState(){
    return {
      viewOne: true
    }
  },

  changeView(){
     this.setState({
       viewOne: !this.state.viewOne
     })
  },

  render(){
    if(!this.state.viewOne) return <newView changeView={ () => this.changeView() } />
    return(
      <View>
        <Button onPress={ () => this.changeView() }> change view </Button>
      </View>
    )
  }
})

var newView = React.createClass({
  render(){
    return(
      <View>
        <Text onPress={this.props.changeView}> the View is now changed </Text>
      </View>
    );
  },
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React Native - how to change style and image of a view onPress

From Dev

How to get onPress event from ScrollView Component in React Native

From Dev

How do I bind the storeId to an onPress event in React-Native?

From Dev

react-native-maps - polygon onPress event

From Java

React Native: View onPress does not work

From Dev

Button change style after onPress with React Native

From Dev

this.state is undefined during onPress event in react native

From Dev

onPress change fragment view

From Dev

Change Button Color onPress (toggle functionality) React Native

From Dev

Change view in Navigator in React Native

From Java

How to change to color of react-native-tab-view?

From Java

react native how to call multiple functions when onPress is clicked

From Dev

React Native Hamburger onPress Issue

From Dev

react native passing onPress with value

From Dev

React Native,not correct working onPress

From Dev

React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

From Dev

Change props of parent view in react native

From Dev

React Native iOS Native View send event with proper reactTag

From Java

React Native onPress being called automatically

From Dev

React Native: onPress on component doesn't function

From Dev

React Native: TouchableOpacity onPress problems inside a ScrollView

From Dev

React-Native ListView onPress - undefined is not an object

From Dev

Render a component onPress(TouchableOpacity/Button) in React Native

From Dev

React-Native TouchableHighlit OnPress function

From Dev

TouchableHighLight onPress inside a map function in react native

From Dev

React Native navigation with onPress function in for loop

From Dev

How to handle onPress function react

From Dev

React native: How to individually control each onpress() from map() generated components

From Dev

How to export View to image data in React Native

Related Related

  1. 1

    React Native - how to change style and image of a view onPress

  2. 2

    How to get onPress event from ScrollView Component in React Native

  3. 3

    How do I bind the storeId to an onPress event in React-Native?

  4. 4

    react-native-maps - polygon onPress event

  5. 5

    React Native: View onPress does not work

  6. 6

    Button change style after onPress with React Native

  7. 7

    this.state is undefined during onPress event in react native

  8. 8

    onPress change fragment view

  9. 9

    Change Button Color onPress (toggle functionality) React Native

  10. 10

    Change view in Navigator in React Native

  11. 11

    How to change to color of react-native-tab-view?

  12. 12

    react native how to call multiple functions when onPress is clicked

  13. 13

    React Native Hamburger onPress Issue

  14. 14

    react native passing onPress with value

  15. 15

    React Native,not correct working onPress

  16. 16

    React Native - Handle onPress event to show image while specific ListView Pressed using Timeline ListView

  17. 17

    Change props of parent view in react native

  18. 18

    React Native iOS Native View send event with proper reactTag

  19. 19

    React Native onPress being called automatically

  20. 20

    React Native: onPress on component doesn't function

  21. 21

    React Native: TouchableOpacity onPress problems inside a ScrollView

  22. 22

    React-Native ListView onPress - undefined is not an object

  23. 23

    Render a component onPress(TouchableOpacity/Button) in React Native

  24. 24

    React-Native TouchableHighlit OnPress function

  25. 25

    TouchableHighLight onPress inside a map function in react native

  26. 26

    React Native navigation with onPress function in for loop

  27. 27

    How to handle onPress function react

  28. 28

    React native: How to individually control each onpress() from map() generated components

  29. 29

    How to export View to image data in React Native

HotTag

Archive