I have a chat app, when I create for the first time a new chat I need an random Id to identify the chat, then, when I try to send the message I need that same id. I declare nanoId before the component declaration:
import "react-native-get-random-values";
import { nanoid } from "nanoid";
const randomId = nanoid()
export default function Chat({navigation, route}) {
const [messages, setMessages] = useState([]); //messages array
const [roomHash, setRoomHash] = useState("");
const {user} = useUserAuth(); //authenticated user from the context provider
const contact = route.params.contact;
const room = route.params.room;
console.log(randomId)
const roomId = room ? room.id : randomId`
All works fine, I create the chat correctly, I can even send the messages correctly because it is kept with the same id that was used to create the chat.
the problem comes when I navigate to home and try to create a new second chat, the problem is it keeps the first ID, it is like it never change after the first time. So, how can I get a new random id every time I open the chat screen?