SortableJS / react-sortablejs

React bindings for SortableJS
http://sortablejs.github.io/react-sortablejs/
MIT License
2.03k stars 210 forks source link

How to pass props using Custom Component #163

Open aquaductape opened 4 years ago

aquaductape commented 4 years ago

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>
Tammy-zting commented 2 years ago

How to do?

andresin87 commented 2 years ago

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

pillys commented 2 years ago

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

GO-Lorenzo commented 2 years ago

I'm facing the same issue.

abder commented 6 months ago

Facing the same issue here

Tammy-zting commented 6 months ago

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