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

In react, how to receive and render data in subpage #244

Open
OwenQC opened this issue Dec 19, 2023 · 2 comments
Open

In react, how to receive and render data in subpage #244

OwenQC opened this issue Dec 19, 2023 · 2 comments

Comments

@OwenQC
Copy link

OwenQC commented Dec 19, 2023

Does Postmate.Model's options can monitor but cant change data in react?

My need is to monitor the data sent by the parent page in the subpage(iframe), and display the data on the subpage after receiving.
The childAPI in Postmate.Model().then() does not have an "on" function like new Postmate().then(), so I put the eventListener-"count" in options, when I get the data and use setState to change it, data can be changed, but the dom does not render this data .
T-T。

Am I use the wrong way?

// iframe
export default function View() {
const [countFromParent, setCountFromParent] = useState(0);
useEffect(() => {
new Postmate.Model({
count: (data) => {
console.log("received:", data) // data has received
setCountFromParent(data);
}
});
}, [])
console.log("countFromParent:", countFromParent) // countFromParent has changed
return (
// but there's actually no rendering
<h1> {countFromParent }</h1>
);
}

@OwenQC
Copy link
Author

OwenQC commented Jan 10, 2024

It's not about react. This is because the iframe configured in the parent page is automatically generated instead of using an existing iframe.So no changes will be made to the iframe already on the page.

@Nine-Rice
Copy link

I have encountered a scenario that can reproduce this situation:

const CONTAINER_ID = 'container_id'
new Postmate({
    container: document.getElementById(CONTAINER_ID),
    url:'xxx',
    name: 'xxx',
});

// ❌ Actually, it's not necessary to use the<iframe>tag
const ParentPage = () => {
  return (
      <div> 
         <iframe id={CONTAINER_ID}></iframe>
      </div>
  )
}

// ✅
const ParentPage = () => {
  return <div id={CONTAINER_ID}></div>
}

Because the source code will automatically add iframe elements to your container:
https://github.com/dollarshaveclub/postmate/blob/f267096b59d469757932dd14c41f72b5c3341413/src/postmate.js#L274

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

No branches or pull requests

2 participants