React Native & Sendbird - invalid parameter

452 Views Asked by At

So, I'm using the sendbird chat API (JS) with react native and the first time the component loads I get an error(Sendbird invalid parameter), but when I save the file and metro reloads it connects successfully:

var sb;
const [idRetrieved, setIdRetrieved] = useState("");
useEffect(() => {
    let id;
    const getUser = async() => 
    {
        id = await AsyncStorage.getItem("idStored");
        id = `u_${id}`;
        setIdRetrieved(id);
    }
    getUser();
    console.log(`user_id: ${id}`);
    let sbConnect = () => {
        sb = new SendBird({appId: MyAppID, localCacheEnabled: false });   // The `localCacheEnabled` is optional.
        sb.connect(idRetrieved, function(user, error) {
            if (error) {
                // Handle error.
                console.log(error);
                
            }
            else
            {
                console.log("Connected:");
                console.log(user);
            }
            // The user is connected to Sendbird server.
        });
    }
    sbConnect();
},[]);

Also if I directly pass in a string instead of idRetrieved it works fine too and logs "Connected":

 sb.connect("u_1", function(user, error) {
1

There are 1 best solutions below

0
On

So all I had to do was put the getUser function inside a useEffect with componentDidMount and the sbConnect with a useEffect that has [idRetrieved] as the dependancy array as mentioned in the comment above.