Home > database >  how to do this in functional component React Native
how to do this in functional component React Native

Time:09-29

I need to add this dropdown into my React Native App. I have a functional component and the tutorial I found is in the Class component. So really appreciate anyone who could help me convert the following into a functional component.

import Dropdown from 'react-native-modal-select-option';

const propsDropdown = {
  defaultValue: {value: 5, label: 'Kebumen'},
  options: [
    {value: 1, label: 'Bandung'},
    {value: 2, label: 'Surabaya'},
    {value: 3, label: 'Palembang'},
    {value: 4, label: 'Jakarta'},
  ],
  label: 'Your City',
  animationType: 'none',
};

type State = {
  selectedOption: Object,
  isShowingOptions: boolean;
};

export default class DropdownDemo extends Component {
  state: State;
  constructor() {
    super(...arguments);
    this.state = {
      selectedOption: propsDropdown.defaultValue || {value: 0, label: 'Pilih Kota'},
      isShowingOptions: false,
    };
  }

  render() {
    let {isShowingOptions, selectedOption} = this.state;
    return (
      <View style={{flex: 1, padding: 10, paddingTop: 50}}>
        <Dropdown {...propsDropdown}
          onSelect={this._onSelect.bind(this)}
          onShow={this._onShow.bind(this)}
          isShowingOptions={isShowingOptions}
          selectedOption={selectedOption}
        />
      </View>
    );
  }
  _onShow(value: boolean): void {
    this.setState({
      isShowingOptions: value,
    });
  }
  _onSelect(item: Object, isShow: boolean): void {
    this.setState({
      isShowingOptions: isShow,
      selectedOption: item,
    });
  }
}
AppRegistry.registerComponent('SandboxRn', () => DropdownDemo);

CodePudding user response:

For Functional component you can use react Native hook.useState for managing state anduseEffect for doing for managing lifecycle callback.

CodePudding user response:

This is how you can do it.

import React, {useState} from 'react';
import Dropdown from 'react-native-modal-select-option';

const propsDropdown = {
  defaultValue: {value: 5, label: 'Kebumen'},
  options: [
    {value: 1, label: 'Bandung'},
    {value: 2, label: 'Surabaya'},
    {value: 3, label: 'Palembang'},
    {value: 4, label: 'Jakarta'},
  ],
  label: 'Your City',
  animationType: 'none',
};

const DropdownDemo = () => {
  
  const [isShowingOptions, setIsShowingOptions] = useState(false);
  const [selectedOption, setSelectedOption] = useState(
    propsDropdown.defaultValue || {value: 0, label: 'Pilih Kota'}
  );

  _onShow(value: boolean): void {
    this.setState({
      isShowingOptions: value,
    });
  }
  _onSelect(item: Object, isShow: boolean): void {
    this.setState({
      isShowingOptions: isShow,
      selectedOption: item,
    });
  }

    return (
      <View style={{flex: 1, padding: 10, paddingTop: 50}}>
        <Dropdown {...propsDropdown}
          onSelect={_onSelect}
          onShow={_onShow}
          isShowingOptions={isShowingOptions}
          selectedOption={selectedOption}
        />
      </View>
    );
}

export default DropdownDemo;
AppRegistry.registerComponent('SandboxRn', () => DropdownDemo);

Hope this works

  • Related