Issue with the react-native-autocomplete-input where the list does not close after selecting an option

After selecting an item from the list in my react-native-autocomplete-input, the list does not close.

let Location = (props) => (

  <View style={styles.formItem}>

    <Autocomplete
      data={props.autocompleteResults.predictions}
      defaultValue={props.locationInput.toString()}
      onChangeText={
        text => {
          props.updateLocationInput(text)
          props.getAutocompleteResults(text)
        }
      }
      renderItem={place => (
        <TouchableOpacity
          style={{
            height: 44,
            flex: 1,
            justifyContent: 'center',
            ...styles.label,
            borderRadius: 8
          }}
          onPress={() => {
            console.log(place)
            props.updatePlace(place)
            props.updateLocationInput(place.description)
          }}>
          <Text numberOfLines={1}>{place.description}</Text>
        </TouchableOpacity>
      )}
      inputContainerStyle={{ borderWidth: 0 }}
      style={{
        height: 44,
        borderRadius: 8,
        backgroundColor: '#FFFFFF',
        alignSelf: 'stretch',
        paddingLeft: 10,
        position: 'relative',
        ...styles.label
      }}
    />
  </View>
)
Location.propTypes = {
  locationInput: React.PropTypes.string.isRequired,
  updateLocationInput: React.PropTypes.func.isRequired,
  getAutocompleteResults: React.PropTypes.func.isRequired,
  autocompleteResults: React.PropTypes.object.isRequired,
  updatePlace: React.PropTypes.func.isRequired
}

Location = connect(
  mapStateToProps,
  mapDispatchToProps
)(Location)

This is how the Location component is utilized. Container pertains to a native-base component:

<Container style={styles.container}>
        <ScrollView keyboardShouldPersistTaps={true}>
          <Categories />
          <View style={{ zIndex: 2, position: 'relative' }}>
            <Location />
          </View>
          <Keywords />
          <Button block style={styles.wideButton} onPress={() => props.toggleMenu()}>
            <Text>GO</Text>
          </Button>
        </ScrollView>
      </Container>

The Location component is embedded within a ScrollView, yet the issue persists even when removing the scrollview. I have also applied the scroll view fix

<ScrollView keyboardShouldPersistTaps={true}>
. What could be causing the list to remain open?

Answer №1

Let's test out the following approach:

// Check if data is available before displaying it
data={(!props.autocompleteResults.predictions.length || props.clicked)? [] : props.autocompleteResults.predictions }

onChangeText={
        text => {
          props.updateLocationInput(text)
          props.getAutocompleteResults(text)
          props.changeClick(false)
        }
      }

onPress={() => {
            console.log(place)
            props.updatePlace(place)
            props.updateLocationInput(place.description)
            props.changeClick(true) 
          }}

Inside the component:

constructor(props)
{
  super(props);
  this.state={ clicked : false }
}

<Location clicked = {this.state.clicked} changeClick={(isClicked) => this.setState({clicked: isClicked})} />

Cheers :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing unauthorized access to files in ExpressJS public directories

Is there a way to conceal files served by the Node server? Despite my attempts to redirect certain files and directories, Express 4.X does not seem to cooperate. I have also experimented with sending 4XX HTTP responses when specific files are requested, bu ...

Initiate a file download following the redirection of a user to a new page in NodeJS/Express

Overview I am currently developing a NodeJS project using Express. In the application, there are buttons visible to the public that trigger file downloads. These buttons are linked to protected routes which will redirect users to a login page if they are ...

Ingesting RSS feed into an Express server

I've been searching for hours, but I just can't seem to find a solution. I was able to figure things out when working on the client side, but now that I'm trying to load posts on the server and render them in the view, I'm hitting a roa ...

"Implementing drag and drop functionality in Vue.js without the need for a

Exploring html 5 drag and drop functionality using vue js has been a challenging experience for me. After following the w3schools tutorial on drag and drop, I managed to get it working in a basic html file but faced difficulties when implementing it in my ...

Transforming an array into a JSON object

I currently have an array structured like this: [ 'quality', 'print-quality: 4', 'x-dimension', 'Value: 21590', 'Value: y-dimension', 'Value: 27940', 'Value: ', 'Valu ...

Switching the cursor to an image when hovering over an element is causing inconsistency in hover events triggering

Currently, I am attempting to implement an effect that changes the cursor to an image when hovering over a text element and reverts back to the normal cursor upon leaving the text element. However, this functionality is not working as expected when using R ...

Tips for adjusting the vertical position of an image within a bootstrap column by a small amount

Apologies in advance if this question has already been addressed, and I am struggling to adapt it to my specific scenario. My objective is to adjust the positioning of the two images shown in the screenshot example below so that they align with the grey b ...

What causes variations in the visual results when components are placed in different <Route> positions within <Switch>?

I recently followed the React Router guide available at https://reacttraining.com/react-router/web/guides/quick-start and decided to implement the code provided: import React from "react"; import { BrowserRouter as Router, Switch, Route, Link } from "reac ...

Issue with data updating in Angular rxjs with share, map, and filter functions

After gathering search criteria from a form, I have developed a method that retrieves an observable of talents. fetchTalents(searchCriteria) { return this._allUsers$.pipe( tap((talents) => console.log(talents)), map((tale ...

What is the process for dynamically checking in a node in jstree?

I am utilizing Jstree from https://github.com/vakata/jstree. I have successfully loaded the tree structure, and now I want to bind checked checkboxes from an array of data. By default, the nodes have unique ids. I will check the id of each node in the arra ...

Should I only store one piece of state data in the state if multiple pieces always change together, and keep the rest in class variables instead?

Struggling to grasp what exactly needs to be maintained in state for plain React. Imagine having a main component <App> (holding all the state) and several other nested display components (only displaying passed props without any state). It appears ...

Once you address a block using jQuery

$(function(){ $(window).scroll(function () { var scrollVal = $(this).scrollTop(); var adscrtop =$(".header").offset().top // 在 RWD 767以下不作動 if(window.screen.width>767){ if(sc ...

Unidentified object type detected - Material UI

This snippet of code was taken directly from the React Material UI website <Select id="selectedSubusecases" multiple value={stepsState.campaignOverviewStep.selectedSubUsecases} ...

Issue with background/hero video not adjusting its size

I've been experimenting with creating a page that features a video as the background, but no matter what I try, the video never seems to resize properly. The image below shows how it looks: image in question body { margin: 0; } .hero-se ...

Encountering obstacles when attempting to save form information to a MongoDB database using Node.js

I've been struggling to save the data entered in my form to my database, but it's not working at all. No errors are showing up and nothing is being logged. Here's the HTML code I'm using: <!DOCTYPE html> <html lang="en"> & ...

Django Ajax filter displaying issue on HTML page

I'm uncertain about the correctness of my Ajax implementation. When using Django's built-in tags, the objects I pass through Ajax are not appearing on my template HTML page. view_results.html <div> <input id="search" name="search" t ...

In the context of ReactJS, how should the src property be formatted within an img tag to reference a mapped json file correctly?

Currently, I am trying to incorporate images from a local JSON file into my component. These images need to correspond with the data obtained from an API call. The contents of my JSON file are as follows: [ { "id": 1, "dwarf" ...

How to Access the Clicked Marker in React Leaflet

I'm currently working on a React project with a leaflet map. The map has markers, and I want to be able to show the information from a clicked marker in a side panel. How can I achieve this functionality? The regular onClick method doesn't seem t ...

Invoke a function in a different component following the completion of an asynchronous task in a React.js application

I am working with 2 components in my project. The first component contains a function that needs to be called after an async function in the second component completes. I am looking for a way to achieve something similar to Vue's this.$emit() function ...

What sets apart these two JavaScript namespaces?

My main goal is to expertly organize my javascript code by eliminating any global elements. I came across two namespace declaration methods in this informative post, and now I'm looking for your input on the advantages and disadvantages of each. ...