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

How can I pass props from the Conversation object to to the MessageList object? #32

Closed
SuppSoftAdminZ opened this issue Apr 17, 2021 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@SuppSoftAdminZ
Copy link

Looking at this example:
https://chatscope.io/storybook/react/?path=/docs/components-maincontainer--default-story
If a user clicks one of the conversation objects on the left (E.g. Lilly: Yes I can do that), we want the Message List on the right to be populated with the list of Messages for that Conversation.

I see there is an onClick event for the Conversation object but I can't tell which property to use in the payload that gets passed from the onClick event. And also, how to pass that payload to the MessageList object.

@supersnager supersnager added the question Further information is requested label Apr 19, 2021
@supersnager supersnager self-assigned this Apr 19, 2021
@supersnager
Copy link
Contributor

@SuppSoftAdminZ Each conversation usually has a unique id. It can be a conference id or a user id depending on your implementation. You can use this id in the onClick handler.
How you keep the state in your application is up to you. For example you can use the local useState/useReducer, Redux, or React context.

Here is an example using the local state.

const conversations = [
  { id: 0, name: "Joe" },
  { id: 1, name: "Lilly" },
  { id: 2, name: "Patrick" }
];

// Messages are indexed by conversation id
const = messages = {
  1: [
    { message: "message 1"}, 
    { message: "message 2"}
  ],
 2: [
    { message: "message 3"}, 
    { message: "message 4"}
  ]
....
}

const [currentConversation, setCurrentConversation] = useState(0);

<ConversationList>
  {conversations.map( c => <Conversation key={c.id} name={c.name}
                                         onClick={() => setCurrentConversation(c.id)} />)  }
</ConversationList>

// Render message list from current conversation
<MessageList>
  {messages[currentConversation].map( (m,idx) => <Message key={idx} model={m} />)}
</MessageList>

This is a naive implementation, but you get the idea.

Good news:

This week I will be releasing a beta version of my new chat state management library.
It's based on react context and hooks.

It has some nice features:

  • ready to use methods for adding messages
  • tracking state of grouped messages
  • tracking state of conversations and current conversation
  • support for debounced typing indicator
  • and so on....

I think this library will be useful for you.

@SuppSoftAdminZ
Copy link
Author

Thanks!! I ended up using this on in the onClick event and it works perfectly.
onClick={() => convoClicked(node.messageId,node.messageSubject)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants