reactjsreact-final-formreact-final-form-arrays

How can I reverse the order that items are added to the fields in final-form/react-final-form-arrays so most recent is first


Im using final-form/react-final-form-arrays library is there a way to sort by most recent field appearing first. The FieldArray component has the fields prop which is array like but does not have a reverse function. So each time I add a new field e.g fields.push({newObject}) I want that rendered component to be at the top of the list. Anyone know how to make this work?


Solution

  • Yes, there is a way to add values to the top instead of the bottom.

    Simply replace

    fields.push({});
    

    with

    fields.unshift({});
    

    More details can also be found in the root readme: https://github.com/final-form/react-final-form-arrays