Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS |失败的Prop Type预期字符串改为get对象_Javascript_Reactjs_React Select_React Proptypes_Formik - Fatal编程技术网

Javascript ReactJS |失败的Prop Type预期字符串改为get对象

Javascript ReactJS |失败的Prop Type预期字符串改为get对象,javascript,reactjs,react-select,react-proptypes,formik,Javascript,Reactjs,React Select,React Proptypes,Formik,我正在使用React Select,以我的形式与Formik一起使用。我有以下一组对象,它们来自API,我通过API映射它们,以便以React select所需的格式转换数据 下面是映射到的数据数组,我提供给select [ { "label": "picard", "value": "picard" }, { "label": "riker", "value": "riker" } ] 它有一个标签和值对。就像你想要的那样。但是在我的Formik HOC中,我将数据属性ye.g:groups

我正在使用React Select,以我的形式与Formik一起使用。我有以下一组对象,它们来自API,我通过API映射它们,以便以React select所需的格式转换数据

下面是映射到的数据数组,我提供给select

[ { "label": "picard", "value": "picard" }, { "label": "riker", "value": "riker" } ]
它有一个标签和值对。就像你想要的那样。但是在我的Formik HOC中,我将数据属性ye.g:groups的值的PropType设置为字符串,因为服务器将给我和接收唯一的字符串

以下是PropType:

      values: PropTypes.shape({
        useraname: PropTypes.string,
        email: PropTypes.string,
        password: PropTypes.string,
        confirmPassword: PropTypes.string,
        group: PropTypes.string
      }),
因此,每次我尝试从下拉列表中选择一个项目时,我都会得到以下结果:

checkPropTypes.js:19 Warning: Failed prop type: Invalid prop `values.group` of type `object` supplied to `AddEditUser`, expected `string`.
以下是select元素和映射函数:

  mapListGroupToSelect = () => {
    const { groups } = this.state;
    return groups.map(group => ({
      label: group.name,
      value: group.name
    }));
  };

  const groups = this.mapListGroupToSelect();

  return (
          <div className="pt-2">
            <label className="font-weight-bold">
              Select Group <Asterisk />
            </label>
            <Select
              placeholder="Select a Group (Just One)"
              onChangeCallback={handleChange}
              type="simpleSelect"
              options={groups}
              isMulti={false}
              id="group"
              onChange={options => setFieldValue('group', options)}
            />
            {JSON.stringify(groups, null, 2)}
            <ErrorMessage name="group" component="div" className="text-danger" />
          </div>
)

你对如何解决这个问题有什么想法吗?

FYI,JSON.Stringify对组不起作用。它会向react-select抛出一个有关意外道具的错误。谁在定义道具类型值-您,还是第三方代码?我看不到您在任何地方传递值,所以我假设它来自react-select?值由react-select传递到Formik HOC。。。问题是react select需要一个对象才能工作,而API和Formik需要一个字符串。。。所以我想问的是如何使这两个条件一起工作……仅供参考,JSON.Stringify在组上不起作用。它会向react-select抛出一个有关意外道具的错误。谁在定义道具类型值-您,还是第三方代码?我看不到您在任何地方传递值,所以我假设它来自react-select?值由react-select传递到Formik HOC。。。问题是react select需要一个对象才能工作,而API和Formik需要一个字符串。。。所以我在问如何使这两个条件一起工作。。。