tajo / react-range

🎚️Range input with a slider. Accessible. Bring your own styles and markup.
https://react-range.pages.dev
MIT License
859 stars 97 forks source link

A props object containing a "key" prop is being spread into JSX: #188

Closed drokbers closed 3 months ago

drokbers commented 1 year ago

My Code:

const CustomRange: React.FC<CustomRangeProps> = ({
  value,
  step,
  min,
  max,
  onChange,
}) => {
  return (
    <Range
      values={value}
      step={step}
      min={min}
      max={max}
      onChange={onChange}
      renderTrack={({ props, children }) => (
        <div
          onMouseDown={props.onMouseDown}
          onTouchStart={props.onTouchStart}
          className="w-full h-7 flex group"
          style={props.style}
        >
          <div
            ref={props.ref}
            className="h-1 w-full rounded-md self-center"
            style={{
              background: getTrackBackground({
                values: value,
                colors: ["#ffff", "#0c0a09"],
                min,
                max,
              }),
            }}
          >
            {children}
          </div>
        </div>
      )}
      renderThumb={({ props, isDragged }) => (
        <div
          {...props}
          className={`h-3 w-3 rounded-full bg-white ${
            !isDragged ? "opacity-0" : ""
          } group-hover:opacity-100`}
          style={{
            ...props.style,

            boxShadow: "0px 2px 6px #AAA",
          }}
        ></div>
      )}
    />
  );

Error:

image




First of all, I checked if there is a key in the props object and if there is, I thought of defining it separately as follows :

  values={value}
  step={step}
  min={min}
  max={max}
  onChange={onChange}
  renderTrack={({ props, children }) => (

    <div
      onMouseDown={props.onMouseDown}
      onTouchStart={props.onTouchStart}
      className="w-full h-7 flex group"
      style={props.style}
    >

      <div
        ref={props.ref}
        className="h-1 w-full rounded-md self-center"
        style={{
          background: getTrackBackground({
            values: value,
            colors: ["#ffff", "#0c0a09"],
            min,
            max,
          }),
        }}
      >
        {children}
      </div>
    </div>
  )}

  renderThumb={({ props, isDragged }) => (
    <div
      {...props}
      className={`h-3 w-3 rounded-full bg-white ${
        !isDragged ? "opacity-0" : ""
      } group-hover:opacity-100`}
      style={{
        ...props.style,

        boxShadow: "0px 2px 6px #AAA",
      }}
    ></div>
  )}
/>`


Then it gave an error "message": "Property 'key' does not exist on type 'ITrackProps'.", so renderTrack and renderThumb callbacks do not have a property named key.

How can I solve this problem?

ljk1291 commented 1 year ago

@drokbers Im having a similar issue after switching to app router on nextjs, did you manage to find a solution?

drokbers commented 1 year ago

@drokbers Im having a similar issue after switching to app router on nextjs, did you manage to find a solution?

I couldn't solve the error, but when I deploy it to Vercel, it's not giving an error. It only gives an error when I'm working locally.

sirldev commented 9 months ago

You just pass key={props.key} at renderThumb

eg


renderThumb={({ props, isDragged }) => (
    <div
      {...props}
     key={props.key}
      className={`h-3 w-3 rounded-full bg-white ${
        !isDragged ? "opacity-0" : ""
      } group-hover:opacity-100`}
      style={{
        ...props.style,

        boxShadow: "0px 2px 6px #AAA",
      }}
    ></div>
  )}
tajo commented 3 months ago

Example updated.