react-sortablejs icon indicating copy to clipboard operation
react-sortablejs copied to clipboard

How to pass props using Custom Component

Open aquaductape opened this issue 4 years ago • 8 comments

I'm running into a situation where I need to pass a prop to the CustomComponent. I've looked through the documentation and I couldn't find a solution, I'm hoping you guys know something and I might have missed it.

import React, { FC, useState, forwardRef } from "react";
import { ReactSortable } from "react-sortablejs";

// This is just like a normal component, but now has a ref.
const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => {
 // grandProp needs to be used here
 const someFunc = () => props.granProp
  return <div ref={ref}>{props.children}</div>;
});

export const BasicFunction: FC = ({grandProp}) => {
  const [state, setState] = useState([
    { id: 1, name: "shrek" },
    { id: 2, name: "fiona" }
  ]);

  return (
    // how to pass grandProp to custom tag?
    <ReactSortable tag={CustomComponent} list={state} setList={setState}>
      {state.map(item => (
        <div key={item.id}>{item.name}</div>
      ))}
    </ReactSortable>
  );
};

If there's no solution, then I'll fork, and create an option that passes props, that could look something like this

<ReactSortable 
tag={CustomComponent} 
list={state} setList={setState} 
passProps={{grandProp}}>
...
<ReactSortable>

aquaductape avatar Jun 14 '20 07:06 aquaductape

How to do?

Tammy-zting avatar Sep 26 '21 00:09 Tammy-zting

The tag prop type is ReactHTML. The only possible values are HTMLAnchorElement. Does not admit ReactComponents. You can add it wrapping ReactSortable.

andresin87 avatar Jan 13 '22 22:01 andresin87

I need too, wrapping is not a good idea, some scene do not need wrapping elements.

pillys avatar Mar 15 '22 07:03 pillys

I'm facing the same issue.

GO-Lorenzo avatar Apr 21 '22 02:04 GO-Lorenzo

Facing the same issue here

abder avatar Mar 07 '24 11:03 abder

周婷婷已收到您的邮件,谢谢!!

Tammy-zting avatar Mar 07 '24 11:03 Tammy-zting