Create a button component : Part 9

To create a button component we will add new file in common folder of src. Button.js

 

We will import React and Component from react and

Text and TouchableOpacity from react-native

 

Touchable opacity so some animation on press so we will use this and later we will call onPress funtion to navigate on another page to view video.

So create Button.js file with this code

 

import React, { Component } from 'react';
import { Text, TouchableOpacity } from 'react-native';

class Button extends Component {
  render() {
    return (
      <TouchableOpacity>
        <Text>View</Text>
      </TouchableOpacity>
    );
  }
}
export default Button;

Now our button code is ready and we will call this button in VideoList helper fuction where we are calling video id in place of button

 

So open VideoList.js

Import button component import Button from ‘../common/Button’;

 

Go to last CardInner component of helper function renderthumbnails() and replace Text with Button component

 

Now refresh simulator and you will see view text there which we were added in Button component

 

Now we are going to process button design so open Button.js and add some styling

 

Styling of Button component

 

const styles = {

buttonstyles: {
  flex: 1,
  alignSelf: 'stretch',
  borderRadius: 4,
  borderColor: '#000',
  borderWidth: 1,
  backgroundColor: '#ff0000'
},
buttonTextstyle: {
  fontSize: 16,
  color: '#FFF',
  paddingTop: 10,
  paddingBottom: 10,
  alignSelf: 'center',
  fontWeight: '600'
}

};


Now call these styles in TouchableOpacity and Text of Button component

return (
      <TouchableOpacity style={styles.buttonstyles}>
        <Text style={styles.buttonTextstyle}>View</Text>
      </TouchableOpacity>
    );

Now refresh simulator and you will see button style is changed to red

 

Now we need to pass Button pass a props when Button component is pressed . When we press button then a props should be pass to TouchableOpacity on press. Button component press will not work on press until we add press function on TouchableOpacity so

So add

<TouchableOpacity onPress={this.props.onPress} style={styles.buttonstyles}>
        <Text style={styles.buttonTextstyle}>View</Text>
</TouchableOpacity>

Now we can make button text more editable so anyone can easily change where they call <Button /> component so we will add children same as we did for Cards components

<TouchableOpacity onPress={this.props.onPress} style={styles.buttonstyles}>
        <Text style={styles.buttonTextstyle}>{this.props.children}</Text>
</TouchableOpacity>

So now button can be called with desired button text like this

<Button> Button Text Here </Button>

 

So Open VideoList.js and change button like this

 

<Button>

View

</Button>

 

Now please reload Simulator and check design

 

So page is ready to display with all video listing. In further process i am going to use navigation to play video on new page with id.

© Copyrights 2014 by STW Services LLP