1

Let me start by saying I am working on my first webpart and learning react spfx. I am trying to render multiple elements using ReactDom.render. How do I render both the elements on the same .ts file? This is a multiscreen webpart which I have realized has only one .ts file running at the initiation.
//ReactDom.render(element, this.domElement);---This is for spfxcontext and oninit ReactDom.render(React.createElement(App), this.domElement);---This is for rendering the Reactrouter headers

How do I declare both in the .ts file? When I use one of them, my header is appearing but spfxcontext and context from init and props are not loading. And when I just use other one, only my list data is populating and my component in app is not loading. I want both of them to be shown when the webpart loads.

Below is the full code:

export default class MultiScreenWebPart extends BaseClientSideWebPart<IReactGetItemsWebPartProps> {
public onInit(): Promise < void> {
console.log("init is running");
return super.onInit().then(_ => {
  sp.setup( this.context);
});
  }
  public render(): void {
console.log("render is running");
const element: React.ReactElement<IReactGetItemsProps> = React.createElement(
  ReactGetItems,
  {
    description: this.properties.description,
    context: this.context
  }
);
//ReactDom.render(element, this.domElement);
ReactDom.render(React.createElement(App), this.domElement);

}

Here is my webpart structure.Link to my github proj. I have the .ts files of no use as they are not rendering when webpart is loading. Only the MultiscreenWebPart.ts is loading.

enter image description here

0

MultiScreenWebPart is your WebPart class which typically will render a single ReactJS component, but then that component can Render whatever you need. You currently have ReactGetItems as your primary component, and within that component class you are already rendering other React components such as Tabs, PeoplePicker and PrimaryButton.

Inside that ReactGetItems, at the top if you import { Header } from './Header' then you can use your custom <Header> component in the Render just like you are using other imported React components. Likewise for your Home, Product etc. components.

  • Thanks @willman I am not trying to import Header into ReactGetitems, I am trying to load App through ReactDom.render(React.createElement(App), this.domElement); which renders the header and other routes work fine.<br> If I wanted to load context, and also have the routes working that is when I am having issue. – Sudheer Chittuluri Feb 13 at 13:24
  • Then you need import App into your ` MultiScreenWebPart` class and change the render method of ` MultiScreenWebPart` to load app instead of reactgetitems. – willman Feb 13 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.