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

Passing props through remote component #1

Open
qlbp opened this issue Feb 2, 2023 · 5 comments
Open

Passing props through remote component #1

qlbp opened this issue Feb 2, 2023 · 5 comments

Comments

@qlbp
Copy link

qlbp commented Feb 2, 2023

Hi there, is there a way to pass props to the remote component, for instance:

//in host/src/Host.jsx
function Host(){
  const world = "world!";
  return <RemoteComponent url="url" scope="scope" module="./App" name={world}/>;
} 
export default Host;

//in remote/src/App.jsx
function App(props) {
  console.log(props);
  return <h2>Hello {props.name}</h2>;
}
export default App;

Currently I believe this would just render Hello as the props object "sent" to App is empty.

@hasanayan
Copy link
Owner

hasanayan commented Feb 2, 2023

This is a valid request; I don't know how I missed this on the initial implementation :)

However, to implement this, I would like to ask for your help because even if I made the change, I can't test this anytime soon. So, could you please open a PR for this?

all changes have to be in RemoteComponent.tsx,

1- add new line after line 10, and add a new property called props -> props?: object;
2- in the component named RemoteComponent at the bottom of the page;
after line 57; add this line props = {},
3- spread props object into the returned component <Component {...props}/>

@qlbp
Copy link
Author

qlbp commented Feb 2, 2023

Thanks for the swift reply, sure I would be more than happy to test this out :)

@qlbp qlbp mentioned this issue Feb 3, 2023
@qlbp
Copy link
Author

qlbp commented Feb 3, 2023

It's in PR, thanks again 👍

@mfreeman-xtivia
Copy link

So is this PR going to be pushed into main?

@hasanayan
Copy link
Owner

sorry guys, I haven't merged this PR because I wanted better type support for the RemoteComponent. it's now a generic component which lets you set the props you want to pass to it with a generic type.

I could not test it myself as the dependencies seem to be quite old and I've been away from CRA world. I don't have time to get back into it now. Could you please install the latest version (0.0.5) and test it?

Here is a usage example

interface MyExtraProps {
  name: string;
}

function App() {
  return (
    <>
      <React.Suspense fallback="loading">
        <RemoteComponent<MyExtraProps>
          url="http://localhost:3002/remoteEntry.js"
          scope="app2"
          module="./Button"
          name="nice button"
        />
      </React.Suspense>
      <React.Suspense fallback="loading">
        <InnerApp />
      </React.Suspense>
    </>
  );

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

3 participants