0

in my app, I have a map of an array that returns a div with a data-date attribute like so:

Note I cannot sort the array here as I don't have any dates to compare them with, only the ID of the element


const mappedArray = array.map(el=>{
  return (
   <DivComponent el={el} />
  )
})

Inside the DivComponent, I make a fetch to firebase where I get the info of each div along with a timestamp

const DivComponent = (props) => {
 const info = useGetInfo(props.el.id) 
 return (
  <div data-data={info.date.toDate()}>Some content</div>
 )
}

Given these conditions, is it possible to sort the array above given that each children have an assigned date ?

1 Answer 1

1

is it okay to change the props on DivComponent and move the useGetInfo outside?

const mappedArray = array.map(el=>useGetInfo(el.id))
.sort((info1,info2)=>info1.date.toDate()>info2.date.toDate()?1:-1)
.map(info=>{
 return (
    <DivComponent info={info} />
  )
})
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.