LoginSignup
0
1

More than 5 years have passed since last update.

React-Native コンポーネント イベント

Last updated at Posted at 2018-06-04

this.props.onXXXXX でイベントコール

CustomButton

this._onClick()}/>

import React, { Component } from 'react';
import {
  View,
  Alert,
} from 'react-native';

// Import native-base UI components
import {
    Button,
    Text,
} from 'native-base';

export default class CustomButton extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
      };
    }

    render() {
      return (
        <View >
            <Button info onPress={this.props.onClick} ><Text>aaaaaa</Text></Button>
        </View>
      );
    }

  }

SlideSwitch

this._onClick()} value={this.state.enabled}/>

import React, { Component } from 'react';
import {
  View,
  Alert,
} from 'react-native';

// Import native-base UI components
import {
  Switch,
} from 'native-base';


import SettingsService from '../lib/SettingsService';


export default class SlideSwitch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      enabled: false,
    };
    //settingsService
    this.settingsService = SettingsService.getInstance();
  }

  render() {
    return (
      <Switch onValueChange={() => this.onValueChange()} value={this.state.enabled} />
    );
  }

  onValueChange(value) {
    this.settingsService.playSound('BUTTON_CLICK');

    //status change
    let enabled = !this.state.enabled;
    this.setState({
      enabled: enabled
    });


    if (enabled) {

      Alert.alert( '', '記録を開始しますか?',
        [
          {text: 'No', onPress: () => {
            this.setState({
              enabled: false,
            });
          }, style: 'cancel'},
          {text: 'Yes', onPress: () => {
            this.onToggleEnabled(enabled);
          }
        },],{ cancelable: false }
      )

    } else {

    }

  }



  onToggleEnabled(enabled) {
    this.props.onValueChange(enabled);
  }

}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1