ホーム 暮らし How to use react-native-picker-select

How to use react-native-picker-select

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 9 分
目次

I have summarized how to use "react-native-picker-select".

Previous

na1229cc22641

1. react-native-picker-select

"react-native-picker-select" is a library for implementing "dropdown lists" (select boxes) in "React Native" apps.

2. How to use react-native-picker-select

The usage of "react-native-picker-select" is as follows.

2-1. Creating a React Native project

(1) Create a React Native project.

npx react-native init my_app
cd my_app

(2) Install the package.
To use "react-native-picker-select", you will also need "@react-native-picker/picker".

npm install react-native-picker-select
npm install @react-native-picker/picker
2-2. iOS setup

(1) Install pods.

cd ios
pod install
cd ..

(2) Sign in Xcode and confirm that it can be installed on an iPhone.

2-3. Implementing the dropdown list

(1) Edit the code.

・App.tsx

import React, useState from 'react';
import  View, Text, StyleSheet, Platform  from 'react-native';
import RNPickerSelect from 'react-native-picker-select';

// アプリ
export default function App() 
  const [selectedValue, setSelectedValue] = useState('TypeScript');

  // UI
  return (
    <View style= flex: 1, justifyContent: 'center', alignItems: 'center' >
      <View style=styles.pickerContainer>
        <RNPickerSelect
          items=[
             label: 'TypeScript', value: 'TypeScript' ,
             label: 'Swift', value: 'Swift' ,
             label: 'Kotlin', value: 'Kotlin' ,
          ]
          placeholder=
            label: 'アイテムを選んでください…',
            value: null,
            color: '#9EA0A4',
          
          value=selectedValue
          style=
            inputIOS: styles.inputIOS,
            inputAndroid: styles.inputAndroid,
          
          => 
            setSelectedValue(value);
          
        />
      </View>
      <Text>selectedValue</Text>
    </View>
  );


// スタイル
const styles = StyleSheet.create(
  pickerContainer: 
    width: '90%',
    borderWidth: 1,
    borderColor: 'gray',
    borderRadius: 8,
    paddingHorizontal: 10,
    paddingVertical: Platform.OS === 'android' ? 0 : 12,
  ,
  inputIOS: 
    fontSize: 16,
    paddingVertical: 12,
    paddingHorizontal: 10,
    color: 'black',
    paddingRight: 30,
  ,
  inputAndroid: 
    fontSize: 16,
    color: 'black',
    paddingRight: 30,
  ,
);

(2) Run the code.

npm start

[Bonus] Handling PhaseScriptExecution

In my environment, "PhaseScriptExecution" occurred when running in Xcode.

(1) Check the error code.
"command not found" is displayed on line 9 of "Script-46EB2E00012EB0.sh".

(2) Check line 9 of "Script-46EB2E00012EB0.sh".
It seems the "$NODE_BINARY" command cannot be found.

"$NODE_BINARY" "$REACT_NATIVE_PATH/sdks/hermes-engine/utils/replace_hermes_version.js" -c "$CONFIG" -r "0.74.4" -p "$PODS_ROOT"

(3) Set the environment variable "$NODE_BINARY" in ".xcode.env" (show hidden files) directly under ios.
I specified the absolute path of node (which node) for "$NODE_BINARY".

# Customize the NODE_BINARY variable here.
For example, to use nvm with brew, add the following line
. "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)
export NODE_BINARY=/Users/npaka/.nodebrew/current/bin/node

(4) Run it again.
It started working without errors.

Next time



同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事