HomeOur TeamContact

Color Picker

By Utkarsh Lubal
Published in Others
May 06, 2023
1 min read
Color Picker

Table Of Contents

01
Introduction
02
Installation
03
Example
04
Tutorial

Introduction

React Native Color Picker is a third-party library that allows you to add a color picker to your React Native app. It offers both uncontrolled and controlled components that you can use to let the user select a color from a color wheel. The ColorPicker component is the uncontrolled version of the color picker, and it has the following props:

• defaultColor: The default color of the color picker. If this prop is not set, the color picker will use black as the default color. • oldColor: The initial color of the color picker. If this prop is set, the color picker will show a preview of the old color, and the user will be able to revert to it by clicking on the “Old” button. • onColorChange: A function that will be called every time the user changes the color. The function will receive the new color as a string in the format “#RRGGBB”. • onColorSelected: A function that will be called when the user selects a color. The function will receive the selected color as a string in the format “#RRGGBB”.

Props

Color pickers accepts properties below. Each property which define color is represented as a color string.

Both color pickers are PureComponents thus if you want to update it you should not mutate its properties deeply.

PropertyTypeNote
colorString\|HSVColor string or HSV object (see below). Defines selected color in controlled component.
defaultColorStringDefines initial selected color in uncontrolled component.
oldColorStringOld color to be used for visual comparision. If it is not defined, whole circle is representing selected color.
styleStyleStyles passed to color picker container
onColorSelectedFunctionCallback with color (HEX string) as argument called when user confirms color selection.
onColorChangeFunctionCallback called each time when color is changed. Used in controlled component. Argument is color in HSV representation (see below)
onOldColorSelectedFunctionCallback with color (HEX string) as argument called when user selects old color.
hideSlidersBooleanOption to hide bottom sliders (holo picker only)
hideControlsBooleanOption to hide bottom buttons (triangle picker only)

Installation

npm install react-native-color-picker --save

And other required dependencies

npm install react-native-paper
npm install @expo/vector-icons

Example

import React from 'react'
import { StyleSheet, View, Text} from 'react-native'
import { TriangleColorPicker, toHsv } from 'react-native-color-picker'

class App extends React.Component {

  constructor(...args) {
    super(...args)
    this.state = { example: null }
  }

  onColorChange(color) {
    this.setState({ color })
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={{color: 'white'}}>React Native Color Picker - Controlled</Text>
       <TriangleColorPicker
          oldColor='purple'
          color={this.state.color}
          onColorChange={this.onColorChange}
          onColorSelected={color => alert(`Color selected: ${color}`)}
          onOldColorSelected={color => alert(`Old color selected: ${color}`)}
          style={{flex: 1}}
        />
      </View>
    )
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 45,
    backgroundColor: '#212021',
  },
})

export default App

Tutorial

Coming Soon…


Previous Article
Confirm Simple Dialog
Utkarsh Lubal

Utkarsh Lubal

Full Stack Developer

Related Posts

Exploring Alternative Emulators for React Native Development
Exploring Alternative Emulators for React Native Development
January 20, 2024
1 min

Quick Links

Advertise with usAbout UsContact Us

Social Media