Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not getting suggestions when used this library with class component #17

Closed
HTEN1998 opened this issue Jan 13, 2021 · 16 comments
Closed
Labels
help wanted Extra attention is needed

Comments

@HTEN1998
Copy link

I am using "react-native": "0.61.5" and when I used this library with class component RenderSuggestions is not getting called.
Can u give me an example of how to use this library with class component

@dabakovich
Copy link
Owner

Did you try something like this?

<MentionInput
  value={value}
  onChange={setValue}

  partTypes={[
    {
      trigger: '@',
      renderSuggestions: (props) => <YourClassComponent {...props} />,
      textStyle: {fontWeight: 'bold', color: 'blue'},
    },
  ]}
/>

@HTEN1998
Copy link
Author

import * as React from 'react';
import { FC, useState, Component } from 'react';
import { Pressable, SafeAreaView, Text, View } from 'react-native';
import { MentionInput, MentionSuggestionsProps, Suggestion } from 'react-native-controlled-mentions';
import Axios from 'react-native-axios';

const users = [
{id: '1', name: 'David Tabaka'},
{id: '2', name: 'Mary'},
{id: '3', name: 'Tony'},
{id: '4', name: 'Mike'},
{id: '5', name: 'harish'},
];

const renderSuggestions1: (suggestions: Suggestion[]) => FC = (suggestions) => (
{keyword, onSuggestionPress},
) => {

if (keyword == null || keyword.length<3) {
return null;
}
else{

    return (
      <View>
        {suggestions
          .filter(one => one.fullname.toLocaleLowerCase().includes(keyword.toLocaleLowerCase()))
          .map(one => (
            <Pressable
              key={one.id}
              onPress={() => onSuggestionPress(one)}
  
              style={{padding: 12,backgroundColor:'cyan'}}
            >
              <Text>{one.name}</Text>
            </Pressable>
          ))
        }
      </View>
    );

}

};

class App extends Component {

constructor(props){
super(props);
this.state = {
inputText:""
}
}

render(){
return(

<MentionInput
value = {this.state.inputText}
onChange = {(input) => this.setState({inputText:input})}
placeholder = "Type here.."
style = {{padding:12}}
partTypes = {[
{
trigger:'@',
renderSuggestions: renderSuggestions(users),
},
{
pattern: /(https?://|www.)[-a-zA-Z0-9@:%.+~#=]{1,256}.(xn--)?[a-z0-9-]{2,20}\b([-a-zA-Z0-9@:%+[],.#?&/=]*[-a-zA-Z0-9@:%_+]#?&/=])*/gi,
textStyle: {color: 'blue'},
}
]}
/>

);
}
};

export default App;

I tried in this mannner

@dabakovich
Copy link
Owner

@HTEN1998 I see on your code renderSuggestions1 but you trying to call renderSuggestions. Is it expected?

@HTEN1998
Copy link
Author

sorry that was a typing mistake, but in code, I was using it as renderSuggestions, can u tell if I m doing that in correct way?

@dabakovich
Copy link
Owner

It looks like it should work. What error are you getting? Where users variable defined?

@dabakovich dabakovich added the help wanted Extra attention is needed label Jan 13, 2021
@HTEN1998
Copy link
Author

I m not getting any error, users variable is defined as a global variable at the top.

const users = [
{id: '1', name: 'David Tabaka'},
{id: '2', name: 'Mary'},
{id: '3', name: 'Tony'},
{id: '4', name: 'Mike'},
{id: '5', name: 'harish'},
];

@dabakovich
Copy link
Owner

Ok, first of all, it shouldn't appear until you will type at least 3 characters due to the keyword.length<3 condition. Or you should remove that condition. Also, you are trying to read fullname that is not present in the users list.

@dabakovich
Copy link
Owner

@HTEN1998, do you have any other questions?

@HTEN1998
Copy link
Author

Yes, currently I m getting suggestions from this library, but I need to make an API call if keyword.length < 3. But before the API call gets finishes, the return statement gets executed

I am trying in this way:

const users = [];

const renderSuggestions: (suggestions: Suggestion[]) => FC = (suggestions) => (
{keyword, onSuggestionPress},
) => {

if (keyword == null || keyword.length<3) {
return null;
}
else{

apiCallingFunction();

return (
  <View>
        {suggestions
          .filter(one => one.name.toLocaleLowerCase().includes(keyword.toLocaleLowerCase()))
          .map(one => (
            <TouchableOpacity key={one.id} onPress={() => onSuggestionPress(one)}>
              <Text>{one.name}</Text>
            </TouchableOpacity>
            
          ))
        }
      </View>
    );

}

};

and I m using Axios library

@dabakovich
Copy link
Owner

You can investigate React hooks, especially useState and useEffect hooks.
Then users state will be like:

const [users, setUsers] = useState([]);

useEffect(() => {
  if (keyword == null || keyword.length<3) {
    setUsers([]);
    return;
  }

  apiCallingFunction().then(newUsers => setUsers(newUsers));
}, [keyword]);

@dabakovich
Copy link
Owner

@HTEN1998 do you have some good news on that?

@HTEN1998
Copy link
Author

@dabakovich sorry for delayed response, your suggestion worked for me. Thanks a lot

@dabakovich
Copy link
Owner

Great, going to close the ticket. Feel free to re-open it if you will get any other questions.

@ricardopacheco
Copy link

ricardopacheco commented May 24, 2021

You can investigate React hooks, especially useState and useEffect hooks.
Then users state will be like:

const [users, setUsers] = useState([]);

useEffect(() => {
  if (keyword == null || keyword.length<3) {
    setUsers([]);
    return;
  }

  apiCallingFunction().then(newUsers => setUsers(newUsers));
}, [keyword]);

@dabakovich I try this and generate warning in development:

Warning: Cannot update a component (`MainEditor`) while rendering a different component (`MentionInput`). To locate the bad setState() call inside `MentionInput`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render

@shahchirag2110
Copy link

@dabakovich @ricardopacheco @HTEN1998 are you guys solve this issue with class component?

@HTEN1998
Copy link
Author

`

const renderSuggestions = ({keyword}) => {
    if (keyword === null) {
        return null;
    }
    const suggestionArrayToRender = sugesstions.filter(item => item.name.includes(keyword));
    return (
        <FlatList
            data={suggestionArrayToRender}
            renderItem={renderSugesstionPopup}
        />
    );
};

render() {
    return (
        <MentionInput
            value={value}
            onChange={setValue}
            partTypes={[
            {
                trigger: '@',
                renderSuggestions,
                textStyle: {color: 'blue'},
            },
            ]}
        />
    );
};

`
This way I implemented, in class component as it is similar to one which is give in documentation, in this i am rendering suggestion UI in seperate function called renderSugesstionPopup()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants