Replies: 2 comments 4 replies
-
Beta Was this translation helpful? Give feedback.
3 replies
-
|
@HeikkiR @annasahola |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Mounting custom components at runtime
I worked out a potential way to mount and use custom components at runtime. You can try it out on this branch.
With it you can use custom components even when using pre-built images of the sampo core.
How to use
You make an extra config folder for your custom components with this structure:

Right now the only custom component in there is a test component
MyCustomComponent, to add more you can add more directories under components with each their own src/index.jsx and webpack.config.js.You then need to build them simply by running
or
You then need to mount the dist directory into your client container like this:
You can refer to custom components in your config json files like this:
How it works
In
webpack.base.js(part of the custom components config) the custom components assign themselves to window.____customComponents. It also marks react as external so that it will reference window.React, meaning the same react as the core application.In
ResultClassRoute.jswe now check a case for custom components.CustomComponentWrapperacts as a wrapper component rendering the component usingloadCustomComponent.When React renders
CustomComponentWrapperfor the first time,loadCustomComponentdynamically injects a <script> tag pointing to/custom-components/MyCustomComponent.js. The browser fetches and executes that script, which writes the component ontowindow.__customComponents. Theonloadhandler then reads it off window, unwraps the.defaultexport, and resolves thepromise. setComponent(() => component)stores it in React state — the function wrapper being necessary because React's state setter treats bare functions as updater callbacks rather than values.Once
Componentis set in state,CustomComponentWrapperre-renders and returns<Component {...props} />. From React's perspective this is completely normal — it just sees a function component. The custom component receives all the props a core app component could use, allowing the custom component developer to do whatever they want.Because the custom component uses
window.React— the same instance the host app exposed — there is only ever one React running. This is important because React hooks track state through a per-instance fiber tree, so two React instances would break hooks entirely.Questions
I passed the props in a way that seemed clean but also gives all the data necessary. It could turn out to be there are some things missing that might make some components impossible to make. Please let me know if I missed anything like that.
Next steps
So far this is only for resultClass components. This same system could also work for facets.
Beta Was this translation helpful? Give feedback.
All reactions