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

144
Views
React Passing Data to Stateful Component from Another Component

Im trying to learn React and am creating a simple dropdown menu thats displays a list of sorting options for a user to choose. Clicking on whichever sort will sort a current HTMl Ul (alphabetical, oldest-newest, etc...)

I have a component called SortSelection that contains the dropdown menu and all the necessary logic to update the ul. Its called like this(inside another component):

      <div className="collectionsSortDropdownContainer">
        <SortSelection
          options={[
            { value: '-publishDate', name: 'Newest' },
            { value: '+publishDate', name: 'Oldest' },
          ]}
          handleSort={this.handleSort.bind(this)}
        />
      </div>

Inside SortSelection.jsx, I currently have it written like this:

const SortSelection = ({
  options, extraClass, handleSort,
}) => {
   //...necessary logic

However, I realize that I should update SortSelection to be a stateful component since I'll be doing some extra UI stuff like displaying the current value of the Sort and adding a blue checkmark on the user-selected sort.

Im having trouble figuring out how to re-write my component to look like this:

class SortSelection extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentSort: currentSort
    };
  }
}

while also ensuring that the three necessary data parameters(options, extraClass, handleSort) are passed through for instantiating the component. How can I do this correctly?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I assume you are trying to use some states inside your SortSelection

You definitely don't need to use a class component for that, functional component will do as well because we have React State Hook

So, in order to make it less complicated for you, just keep using a functional component like you already did:

Just use useState like so:

const { useState } = require("react")

const SortSelection = ({
  options, extraClass, handleSort,
}) => {
  const [currentSort, setCurrentSort] = useState();  //<-- useState hook
   //...necessary logic
   return (
     //...JSX
   )
}

currentSort is now a state, and every time you want to change that state and trigger a re-render, use setCurrentSort

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!