简体   繁体   English

反应:警告:无法为函数组件提供参考。 尝试访问此引用将失败。 你的意思是使用 React.forwardRef() 吗?

[英]React: Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

I'm having an issue in which the three useRefs() ( minValRef , maxValRef , range ) at the top of my code are causing this error.我遇到了一个问题,我的代码顶部的三个useRefs()minValRefmaxValRefrange )导致了这个错误。

I've looked at all the available solution, but none I think pertain to me.我已经查看了所有可用的解决方案,但我认为没有一个适合我。

I've tried wrapping the function in a forwardRef() , but that seems to only work if the ref is being prop'ed in.我试过将函数包装在forwardRef() ,但这似乎只有在 ref 被支持时才有效。

My codepen is below, if that helps.我的代码笔在下面,如果有帮助的话。 Can anyone give me an idea as to why it's refusing to allow these three useRefs() ?谁能告诉我为什么它拒绝允许这三个useRefs()

import React, { useState, useRef, useCallback, useEffect, forwardRef } from 'react';
import Input from './RangeInputCompound';
import './RangeInputClassStyles.css';
let classnames = require('classnames');

function RangeInputComponent({ min, max }) {
  const [sliderValueDisplay, setSliderValueDisplay] = useState({
    leftSliderValue: false,
    rightSliderValue: false,
  });

  const [minVal, setMinVal] = useState(min);
  const [maxVal, setMaxVal] = useState(max);
  const minValRef = useRef(null);
  const maxValRef = useRef(null);
  const range = useRef(null);

  // create a percentage from the value
  const getPercent = useCallback(
    (value) => {
      Math.round(((value - min) / (max - min)) * 100);
    },
    [min, max]
  );

  // Set width of the range to decrease from the left side
  useEffect(() => {
    if (maxValRef.current) {
      const minPercent = getPercent(minVal);
      const maxPercent = getPercent(+maxValRef.current.value);

      if (range.current) {
        range.current.style.left = `${minPercent}%`;
        range.current.style.width = `${maxPercent - minPercent}%`;
      }
    }
  }, [minVal, getPercent]);

  // Set width of the range to decrease from the right side
  useEffect(() => {
    if (minValRef.current) {
      const minPercent = getPercent(+minValRef.current.value);
      const maxPercent = getPercent(maxVal);

      if (range.current) {
        range.current.style.width = `${maxPercent - minPercent}%`;
      }
    }
  }, [maxVal, getPercent]);

  // useEffect(() => {
  //   onChange({ min: minVal, max: maxVal });
  // }, [minVal, maxVal, onChange]);

  function displaySliderValue(e) {
    if (e.type === 'mousedown') {
      setSliderValueDisplay({
        ...sliderValueDisplay,
        [e.target.id]: true,
      });
    } else {
      setSliderValueDisplay({
        ...sliderValueDisplay,
        [e.target.id]: false,
      });
    }
  }

  return (
    <Input>
      <Input.Label>Revenue</Input.Label>
      <Input.RangeField
        type="range"
        name="revenue"
        id="leftSliderValue"
        min={min}
        max={max}
        step="5"
        value={minVal}
        ref={minValRef}
        onChange={(e) => {
          const value = Math.min(+e.target.value, maxVal - 1);
          setMinVal(value);
          e.target.value = value.toString();
        }}
        onMouseDown={displaySliderValue}
        onMouseUp={displaySliderValue}
        className={classnames('thumb thumb--zindex-3', { 'thumb--zindex-5': minVal > max - 100 })}
      ></Input.RangeField>
      <Input.RangeField
        type="range"
        name="revenue"
        id="rightSliderValue"
        min={min}
        max={max}
        step="5"
        value={maxVal}
        ref={maxValRef}
        onChange={(e) => {
          const value = Math.max(+e.target.value, minVal + 1);
          setMaxVal(value);
          e.target.value = value.toString();
        }}
        onMouseDown={displaySliderValue}
        onMouseUp={displaySliderValue}
        className="thumb thumb--zindex-4"
      />
      <Input.Slider className="slider">
        <Input.SliderTrack className="slider__track" />
        <Input.SliderRange ref={range} className="slider__range" />
        {sliderValueDisplay.leftSliderValue && (
          <Input.SliderValue value={minVal} className="slider__left-value">
            {minVal}
          </Input.SliderValue>
        )}
        {sliderValueDisplay.rightSliderValue && (
          <Input.SliderValue value={maxVal} className="slider__right-value">
            {maxVal}
          </Input.SliderValue>
        )}
      </Input.Slider>
      <Input.LimitValueContainer>
        <Input.LimitValue>min $10k</Input.LimitValue>
        <Input.LimitValue>max $200k</Input.LimitValue>
      </Input.LimitValueContainer>
    </Input>
  );
}

export default RangeInputComponent;

https://codepen.io/cjmaret/pen/XWaPrBd https://codepen.io/cjmaret/pen/XWaPrBd

The answer is that "ref" in this case is referring to the styled component, not the DOM node.答案是,在这种情况下,“ref”指的是样式化组件,而不是 DOM 节点。 I needed to use 'innerRef'.我需要使用'innerRef'。 Thanks Colin!谢谢科林!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:无法为 Function 组件提供参考。 尝试访问此 ref 将失败。 你是想使用 React.forwardRef() 吗? - Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 不能为 TypeScript &gt; Function 组件提供 refs。 尝试访问此引用将失败。 你的意思是使用 React.forwardRef() 吗? - TypeScript > Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 无法为 Function 组件提供参考。 尝试访问此 ref 将失败。 你是想使用 React.forwardRef() 吗? - Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? React hook form v7 Function 组件不能给出参考。 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() - React hook form v7 Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef() React Js 错误:Function 组件不能给出参考。 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() 吗? - React Js Error : Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 在 React 中,我收到警告:无法为函数组件提供 refs。 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() 吗? - In React I get the warning: Function component cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 如何摆脱错误功能组件不能被赋予参考。 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() 吗? - How to get rid off error Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() - Attempts to access this ref will fail. Did you mean to use React.forwardRef() 函数组件不能有引用。 你的意思是使用 React.forwardRef() 吗? - Function components cannot have refs. Did you mean to use React.forwardRef()? ReactJS:React-Month-Picker 错误:函数组件不能有引用。 你的意思是使用 React.forwardRef() 吗? - ReactJS: React-Month-Picker Error: Function components cannot have refs. Did you mean to use React.forwardRef()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM