LoginSignup
1
3

More than 5 years have passed since last update.

React Nativeで無効なボタンを作るTips

Last updated at Posted at 2017-01-25

さっそく作ってみる

react-native init は省略

Buttonコンポーネントを読み込んで

import {
  //省略
  Button
} from 'react-native';

disabledオブションをつけるだけ

<Button
 disabled
 onPress={() => console.log('test')}
 title="押せないボタン"
/>

こんな感じのボタンが出来ます。

スクリーンショット 2017-01-25 18.20.19.png

これを使ってちょっとしたアプリを作ってみました

頭を振り絞って思いついたしょうもない例です。

https://gyazo.com/4896e4a07a67f7c832e098eb52048f58

disabledオプションはtrueを渡すと無効falseで有効なボタンになります。

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Button,
  Alert
} from 'react-native';

export default class AwesomeProject extends Component {

  state = {disabled: false}

  render() {
    return (
      <View style={styles.container}>

      <Button
        disabled={this.state.disabled}
        onPress={this.onButtonPress.bind(this)}
        title="1回だけ押せます"
      />

      </View>
    );
  }

  onButtonPress() {
    Alert.alert('このボタンは無効になりました');
    this.setState({disabled: true});
 }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
});

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

ブログでもButtonについての記事を書いたので、もっと知りたい方はどうぞ
http://st--blog.blogspot.jp/2017/01/hello.html

1
3
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
1
3