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

177
Views
What is the best way of dynamically sorting React components based on user settings?

I've come up with a working solution to this, but it not feels good to me. Just take a look please and let me know. Is that acceptable to use it like that?

The example is very simplified, I've needed to provide a bunch of props for each component, that's why I've put sorting function right inside the main one. Basically each sorted component is a part of big formik form.

import React from "react";
import "./styles.css";

function ComponentOne() {
  return <div>One</div>;
}
function ComponentTwo() {
  return <div>Two</div>;
}
function ComponentThree() {
  return <div>Three</div>;
}
function ComponentFour() {
  return <div>Four</div>;
}

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>

      {(() => {
        const components = [
          {
            order: 4,
            component: <ComponentOne />
          },
          {
            order: 3,
            component: <ComponentTwo />
          },
          {
            order: 2,
            component: <ComponentThree />
          },
          {
            order: 1,
            component: <ComponentFour />
          }
        ];

        return components
          .sort((a, b) => a.order - b.order)
          .map((item) => (
            <React.Fragment key={item.order}>{item.component}</React.Fragment>
          ));
      })()}
    </div>
  );
}

Here's a sandbox for this: https://codesandbox.io/s/confident-leavitt-etwyo1?file=/src/App.js

about 4 years ago · Juan Pablo Isaza
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!