Bob
Bob

Reputation: 133

How to fix item.split is not a function

I'm setting up a new function and getting an error: "item.split is not a function"

This is a function for search suggestions so for example if I will search the number 7 or 78 so it will return me the error: "item.split is not a function"

_renderSuggestion = ({ item }) => {
    const splittedName = item.split(' ')
    const splittedSearch = this.state.currentSearch.toUpperCase().split(' ')
    const suggestion = splittedName.map((word, index) =>
      word.toUpperCase().includes(splittedSearch) ? (
        <Text key={index} style={[Style.suggestionText, { color: '#2eb872' }]}>
          {word}{' '}
        </Text>
      ) : (
        <Text key={index} style={Style.suggestionText}>
          {word}{' '}
        </Text>
      )
    )

I expect the output will be correct without this error.

Upvotes: 1

Views: 1191

Answers (1)

Syed
Syed

Reputation: 570

Try

item.toString().split('')

The value supplied to the item has to be converted to a string

Upvotes: 1

Related Questions