通过对象搜索后响应组件的更新状态

我有一个从 reactIcons npm 包https://www.npmjs.com/package/react-icons输出的对象我正在使用 sanity studio从其中一个文件夹中导入所有内容import * as ReactIcons from 'react-icons/fa',我创建了一个文本输入搜索到的值并运行一个搜索函数,该函数对我从 reactIconsfa文件夹中获取的对象的值运行包含,并记录匹配的值。现在我想获取这些值并使用useState()Howerver 更新反应组件我收到以下错误Uncaught ReferenceError: setIcon is not defined,并且 SetIcon 是 useState 数组中的 setter 函数。到目前为止,这是我的代码


import React, { useState } from 'react';

import PropTypes from 'prop-types'

import FormField from 'part:@sanity/components/formfields/default'

import PatchEvent, {set, unset} from 'part:@sanity/form-builder/patch-event'

import * as ReactIcons from 'react-icons/fa'



console.log(ReactIcons);


const createPatchFrom = value => PatchEvent.from(value === '' ? unset() : set(String(value)))


const Example = value => {

  const [icon, setIcon] = useState();


  return (

   <div>{icon}</div>

  );

}


const search = value => {

  console.log(value);

    Object.keys(ReactIcons).map(go => {

    if (go.toLowerCase().includes(value) === true){

      console.log(go);

      setIcon(go);

    }

  })

}




class IconPickerCustom extends React.Component{


    static propTypes = {

        value: PropTypes.string,

        onChange: PropTypes.func.isRequired

    };



隔江千里
浏览 84回答 1
1回答

白板的微信

React 有两类组件,类组件和函数组件(以前称为无状态组件)。当您意识到需要在函数组件中使用状态并且不想将其转换为类组件时,会在函数组件中使用钩子。useState() Hook 允许我们在函数组件中添加状态。类组件//initialize stateconstructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {foo: bar};}//set statethis.setState({foo: baz});//read statethis.state.foo;函数组件(带有 useState() Hook)//initialize stateconst [icon, setIcon] = useState("myInitialValue");//set statesetIcon("myNewValue");//read state{icon}话虽如此,你已经有了一个类组件,所以这里不需要使用钩子。class IconPickerCustom extends React.Component{constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = { icon: "nothing" };}static propTypes = {&nbsp; &nbsp; value: PropTypes.string,&nbsp; &nbsp; onChange: PropTypes.func.isRequired};const createPatchFrom = value => PatchEvent.from(value === '' ? unset() : set(String(value)));const search = value => {&nbsp; &nbsp; Object.keys(ReactIcons).map(go =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ({ go.toLowerCase().includes(value) ?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setState({icon:go}) : null;})&nbsp;}render = () =>{&nbsp; &nbsp; const {type, value, onChange} = this.props&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; <FormField label={type.title} description={type.description}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={value === undefined ? '' : value}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={event => onChange(createPatchFrom(event.target.value))}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ref={element => this._inputElement = element}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </FormField>&nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={event => search(event.target.value)}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; {Object.values(ReactIcons).map(X =>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <X/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; &nbsp; {console.log(ReactIcons.Fa500Px)}&nbsp; &nbsp; &nbsp; &nbsp; <ReactIcons.Fa500Px/>&nbsp; &nbsp; &nbsp; &nbsp; <Example/>&nbsp; &nbsp; &nbsp; </>&nbsp; &nbsp; )}}export default IconPickerCustom;你会想要一些类似的东西。如果您有任何问题,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript