Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Lazy load a React component from an array of objects

I have made for me a Tutorial-Project where I collect various React-Examples from easy to difficult. There is a "switch/case" conditional rendering in App.js, where I - depending on the ListBox ItemIndex - load and execute the selected Component.

I am trying to optimize my React code by removing the "switch/case" function and replacing it with a two dimensional array, where the 1st column contains the Component-Name 2nd column the Object. Further I would like to lazy-load the selected components.

Everything seems to work fine, I can also catch the mouse events and also the re-rendering begins but the screen becomes white... no component rendering.

App.js

import SampleList, { sampleArray } from './SampleList';

class App extends React.Component {
    constructor(props) {
      super(props);
      this.selectedIndex = -1;  
    }
    renderSample(index) {
      if((index >= 0) && (index < sampleArray.length)) {
        return React.createElement(sampleArray[index][1])
      } else {
        return <h3>Select a Sample</h3>;
      }
    }
    render() {
      return (
        <header>
          <h1>React Tutorial</h1>
          <SampleList myClickEvent={ this.ClickEvent.bind(this) }/>
          <p />
          <div>
            <Suspense> /**** HERE WAS MY ISSUE ****/
              { this.renderSample(this.selectedIndex) }
            </Suspense>
          </div>
        </header>
      );
    }
    ClickEvent(index) {
      this.selectedIndex = index;
      this.forceUpdate();
    }
}

SampleList.js

import React from 'react';

const SimpleComponent = React.lazy(() => import('./lessons/SimpleComponent'));
const IntervalTimerFunction = React.lazy(() =>  import('./lessons/IntervalTimerFunction'));

const sampleArray = [ 
    ["Simple Component", SimpleComponent], 
    ["Interval Timer Function", IntervalTimerFunction]
];

class SampleList extends React.Component {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
        this.selectOptions = sampleArray.map((Sample, Index) => 
            <option>{ Sample[0] }</option>
        );
      }
    render() {
        return (
            <select ref={this.myRef} Size="8" onClick={this.selectEvent.bind(this)}>  
                 { this.selectOptions }
            </select>  
        );
    }
    selectEvent() {
        this.props.myClickEvent(this.myRef.current.selectedIndex);
    }
}

export default SampleList;
export { sampleArray };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have several issues in that code:

  • If you use React.lazy to import components dynamically, use Suspense to show a fallback;
  • The select can listen to the change event, and receive the value of the selected option, that is convenient to pass the index in your case;
  • Changing a ref with a new index doesn't trigger a re-render of your components tree, you need to perform a setState with the selected index;
  • I suggest you to switch to hooks, to have some code optimizations;
  • Code:

    import React, { Suspense, useState, useMemo } from 'react';
    const SimpleComponent = React.lazy(() => import('./lessons/SimpleComponent'));
    const IntervalTimerFunction = React.lazy(() => 
          import('./lessons/IntervalTimerFunction'));
    
    const sampleArray = [
      ['Simple Component', SimpleComponent],
      ['Interval Timer Function', IntervalTimerFunction],
    ];
    
    export default function App() {
      const [idx, setIdx] = useState(0);
      const SelectedSample = useMemo(() => sampleArray[idx][1], [idx]);
      const handleSelect = (idx) => setIdx(idx);
    
      return (
        <Suspense fallback={() => <>Loading...</>}>
          <SampleList handleSelect={handleSelect} />
          <SelectedSample />
        </Suspense>
      );
    }
    
    class SampleList extends React.Component {
      constructor(props) {
        super(props);
      }
      selectEvent(e) {
        this.props.handleSelect(e.target.value);
      }
      render() {
        return (
          <select ref={this.myRef} Size="8" onChange={this.selectEvent.bind(this)}>
            {sampleArray.map((sample, idx) => (
              <option value={idx}>{sample[0]}</option>
            ))}
          </select>
        );
      }
    }

Working example HERE

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!