0

I have an array of objects which I create on pressing the add button.The add handler function is as below.

const [inputList,setInputList] = useState([])

const elref=useRef(null)

const add = () => {
setInputList([...inputList,
<div>
<EditContainer

onTextChnage={handleChange}
ref={elref}
/>

</div>
}])}

This create multiple EditContainer elements all of which share the same ref.How can I create refs like this on the fly for a dynamic array of Object which is a state

1 Answer 1

1

editContainerRefs.current will provide you the array of EditContainer component refs.

const [inputList, setInputList] = useState([]);

const editContainerRefs = useRef([])

const add = () => {
  const newRef = React.createRef()
  editContainerRefs.current.push(newRef)
  setInputList([
    ...inputList,
    <div>
      <EditContainer onTextChnage={handleChange} ref={newRef} />
    </div>
  ]);
};
Sign up to request clarification or add additional context in comments.

Comments

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.