SvenKirschbaum / react-stomp-hooks

This repository contain a react library which enables simple access to stomp subscriptions via hooks.
MIT License
59 stars 12 forks source link

Please help me #19

Closed nik1999777 closed 2 years ago

nik1999777 commented 2 years ago

import { useStompClient, useSubscription } from 'react-stomp-hooks'
import { useEffect, useState } from 'react'

import { create_UUID } from '../utils/StringUtils'
import { Button, Typography } from 'antd'
import serializer from '../utils/serializers/serializer'

const uuid: string = create_UUID()

export const Git = ({ content }: any) => {
    return (
        <div style={{ display: 'flex', alignItems: 'center' }}>
            <Typography style={{ color: 'white' }}>
                Github repository created:
            </Typography>
            <Button href='https://github.com/autotests-cloud/AUTO-1150' type='link'>
                {content}
            </Button>
        </div>
    )
}

export const Telegram = ({ content }: any) => {
    return (
        <iframe
            style={{ width: '100%', height: '650px', marginTop: '200px' }}
            id='telegram-post-autotests_cloud-17'
            src={`https://t.me/${content}?embed=1&discussion=1&comments_limit=5&dark=1`}
        ></iframe>
    )
}

export const Subscribing = () => {
    const [lastMessage, setLastMessage] = useState<any>()

    useSubscription(`/topic/${uuid}`, (message: any) => {
        setLastMessage(addSocketEvent(message.body))
    })

    const addSocketEvent = (message: any) => {
        console.log(JSON.parse(message))

        const { contentType, content } = JSON.parse(message)

        switch (contentType) {
            case 'git':
                return <Git content={content} />
            case 'notification':
                return <Telegram content={content} />
        }
    }

    return (
        <>
            <div>{lastMessage}</div>
        </>
    )
}```
The lastmessage state removes the component and replaces it with another one, and I want them to alternate one after another.
What to do?
nik1999777 commented 2 years ago
image image image

To visualize the problem

SvenKirschbaum commented 2 years ago

The lastmessage state removes the component and replaces it with another one, and I want them to alternate one after another. What to do?

It does what you programmed it to do. You are overwriting the state variable with the new value, If you want to show all received messages save an array in the state, and add to it each message you receive. If you want to alternate between them on a time basis you have to program it that way, too.

This is not an issue with the react-stomp-hooks library. Please post general react question in the future to places like Stackoverflow. You can find other more appropriate places here: https://reactjs.org/community/support.html