Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move an Image on click in React Native

I've recently been learning React Native but haven't really found a way to manipulate the DOM.

I've been trying to make it so that if I click on the TouchableHighlight my Image moves down a couple of px, but I have not managed to get it to move yet and I honestly don't know how to go from here.

My onclick function works since it does return the log every time the button is clicked.

As of now I have this:

export default class MainBody extends Component {

  onclick = () => {
    console.log('On click works')
  };

  render() {

    return (

      <ScrollView showsHorizontalScrollIndicator={false} style={Style.horList} horizontal={true}>

        <View >

          {/*i need to move this Image down!*/}
          <Image source={require("./img/example.png")}/>
          <View>
            <Text style={Style.peopleInvited}>This is text</Text>
          </View>

          {/*When clicked on this touchable highlight!*/}
          <TouchableHighlight onPress={this.onclick}}>
            <Image source={require('./img/moveImg.png')}/>
          </TouchableHighlight>
        </View>

      </ScrollView>
}

If someone could help me get past this that would be greatly appreciated. Thank you so much for your time!

like image 639
jasper Avatar asked Mar 30 '17 21:03

jasper


People also ask

How do you change image on button click in React Native?

store the image source in state and do a setState() with new image source on button press. This will rerender the component and you can set the new image from the state.

How do I click an image in React Native?

You can wrap your <Image /> component with TouchableOpacity other Touchable component. And make the image source dynamic using props or state.

How do I display an image Onclick in react?

Open the command line console, then cd into the root of the React project. From there, run yarn start . This will build the React application, and the ImageViewer will now be loaded into the browser. To load a new RasterImage into the ImageViewer, click Choose File and choose an image.

What is onLayout in React Native?

react-native-on-layout is an npm package that you can include in your app using npm i react-native-on-layout or with yarn add react-native-on-layout . It is a View component that will do this onLayout dance for you and then passes the layout values as parameters to a render prop.


1 Answers

There are many ways to do this, but perhaps the easiest way would be using states.

class MainBody extends Component {
    constructor(props) {
        super(props);
        this.state = {
            top: 0 // Initial value
        };
    }
  onclick = () => {
    console.log('On click works')
    this.setState( { top: this.state.top + 5 }) // 5 is value of change.
};


  render() {

    return (

      <ScrollView showsHorizontalScrollIndicator={false} horizontal={true}>

        <View >

          {/*i need to move this Image down!*/}
          <Image style={{top: this.state.top}}source={require("./img/example.png")}/>
          <View>
            <Text>This is text</Text>
          </View>

          {/*When clicked on this touchable highlight!*/}
          <TouchableHighlight onPress={this.onclick}>
            <Image source={require("./img/moveImg.png")}/>
          </TouchableHighlight>
        </View>

      </ScrollView>
  );
  }
}
like image 158
Truefeel Avatar answered Oct 22 '22 16:10

Truefeel