1

I want wrapping module for multi use.

so, I make an ItemComponent

export const DragItem = (props: DragProps) => {
  const [{... }, fooRef] = useFoo({

  })

  return (
    props.children // how can i send fooRef to here??
  )
}

I should send ref to props.children Is it possible?

곽대용
  • 399
  • 2
  • 3
  • 15

2 Answers2

1

check this : https://codesandbox.io/s/gracious-williams-ywv9m You need to use React.cloneElement to attach/pass extra data to children

export const DragItem = (props: DragProps) => {
  const [foo, fooRef] = React.useState({});
  var childrenWithRef = React.Children.map(props.children, function(child) {
    return React.cloneElement(child, { fooRef: fooRef });
  });
  return childrenWithRef;
};
Kais
  • 1,925
  • 3
  • 23
  • 35
0

I got it.

export const DragItem = (props: DragProps) => {
  const [{... }, fooRef] = useFoo({

  })

  return (
    React.cloneElement(props.children, { ref: fooRef })
  )
}
곽대용
  • 399
  • 2
  • 3
  • 15