[英]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()
( minValRef
、 maxValRef
、 range
)导致了这个错误。
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.