Skip to content

Docs | Incorrect example about sending data from Devvit to a webview #179

@virtuallyunknown

Description

@virtuallyunknown

Hey folks. There is some slight issue with the example provided in the documentation:

window.addEventListener('message', (event) => {
  if (event.data.type === 'devvit-message') {
    const { message } = event.data;
    console.log('Received from Devvit:', message);
  }
});

https://developers.reddit.com/docs/webviews#from-devvit-to-web-view

When we send data from Devvit to the webview, it's structured differently, so trying to destructure the event.data here is invalid.

type DevvitMessage = {
    type: 'devvit-message';
    data: {
        message: {
             // actual contents of whatever data I send from devvit backend
        }
    }
}

// the destructuring in the example from the docs does not work
// because there is no message key/prop inside event.data
// instead, the data is in event.data.data.message
// and the top level message has a "devvit-message" type hard-coded

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions