简体   繁体   English

警告:道具类型失败:提供给 `ForwardRef(Slider)` 的道具 `value` 无效

[英]Warning: Failed prop type: Invalid prop `value` supplied to `ForwardRef(Slider)`

I am trying to create a Material UI range Slider which has two values on it.我正在尝试创建一个 Material UI 范围滑块,它有两个值。 It works if I set:如果我设置,它会起作用:

const [value, setValue] = React.useState([5,20]);
   const [value, setValue] = React.useState([val]);
   const handleChange = (event, newValue) => {
       setValue(newValue);
  };

  return (
    <div className={classes.root}>
      <Typography id="range-slider" gutterBottom>
        Temperature range
      </Typography>
      <Slider
        value={value}
        onChange={handleChange}
        valueLabelDisplay="auto"
        aria-labelledby="range-slider"
        getAriaValueText={valuetext}
      />
      <Input value={value} />
    </div>
  );

But if I set a value and assign that to the useState constant like this, it doesn't work.但是,如果我设置一个值并将其分配给这样的 useState 常量,则它不起作用。 Even with the error, I don't understand why:即使有错误,我也不明白为什么:

var val = [5,20]
const [value, setValue] = React.useState([val])

I get this error:我收到此错误:

Warning: Failed prop type: Invalid prop `value` supplied to `ForwardRef(Slider)`.
    in ForwardRef(Slider) (created by WithStyles(ForwardRef(Slider)))

    in WithStyles(ForwardRef(Slider)) (at demo.js:32)

    in div (at demo.js:28)

    in RangeSlider (at index.js:6)

The slider still renders but just with 1 point instead of 2 on it滑块仍然呈现,但只有 1 个点而不是 2 个点

Have you tried removing the [] brackets when passing val to useState.您是否尝试在将 val 传递给 useState 时删除 [] 括号。 The implication I'm getting of the warning is that its expecting an array, but instead it got an array of arrays when [val] was passed.我收到警告的含义是它期待一个数组,但当 [val] 被传递时,它得到了一个数组数组。

In my case, I did not convert the string to numeric value.就我而言,我没有将字符串转换为数值。 The property value expects either a numeric value or an array of numbers.属性值需要一个数值或一个数字数组。 Passing value as string, for example "23" instead of 23, can cause this warning.将值作为字符串传递,例如“23”而不是 23,可能会导致此警告。

暂无
暂无

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

相关问题 警告:道具类型失败:提供给“ForwardRef(FormControl)”的“string”类型的道具“error”无效,预期为“boolean” - Warning: Failed prop type: Invalid prop `error` of type `string` supplied to `ForwardRef(FormControl)`, expected `boolean` 失败的道具类型:提供给“ForwardRef(Grid)”的无效道具“lg” - Failed prop type: Invalid prop `lg` supplied to `ForwardRef(Grid)` React Native +图标:警告:失败的道具类型:无效的props.style键tintColor提供给ForwardRef(Text) - React Native + Icons: Warning: Failed prop type: Invalid props.style key tintColor supplied to ForwardRef(Text) 警告:道具类型失败:提供给“路线”的道具属性无效。 在途中 - Warning: Failed prop type: Invalid prop `component` supplied to `Route`. in Route React Native SearchBar错误:道具类型失败:提供给`ForwardRef(TextInput)`的`array`类型的无效道具`value`,预期`string` - React Native SearchBar Error: Failed prop type: Invalid prop `value` of type `array` supplied to `ForwardRef(TextInput)`, expected `string` 警告:道具类型失败:提供给“图片”的道具“源”无效 - Warning: Failed prop type: Invalid prop `source` supplied to `Image` 警告:道具类型失败:提供给“ListItem”的道具“副标题”无效 - Warning: Failed prop type: Invalid prop `subtitle` supplied to `ListItem` 警告:道具类型失败:提供给“Form(AddComment)”的道具“initialValues”无效 - Warning: Failed prop type: Invalid prop `initialValues` supplied to `Form(AddComment)` 警告:道具类型失败:提供给视图的道具&#39;backgroundcolor&#39;无效: - warning: failed prop type: invalid prop 'backgroundcolor' supplied to view: reactjs 失败的道具类型:提供给“ForwardRef(IconButton)”的“object”类型的无效道具“className”,应为“string” - reactjs Failed prop type: Invalid prop `className` of type `object` supplied to `ForwardRef(IconButton)`, expected `string`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM