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

问题描述 投票:1回答:1

[我有一个从reactIcons npm包https://www.npmjs.com/package/react-icons输出的对象,我正在使用import * as ReactIcons from 'react-icons/fa'从其中一个文件夹导入所有内容,我使用的是sanity studio,我创建了一个文本输入,该文本输入使用搜索到的值并运行搜索在我从reactIcons fa文件夹中抓取的对象的值上运行一个包含函数的函数,并记录匹配的值。现在,我想获取这些值并使用useState()更新react组件。但是,我得到以下错误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
    };

    render = () =>{
        const {type, value, onChange} = this.props
        return (
          <>
            <FormField label={type.title} description={type.description}>
              <input
                type="text"
                value={value === undefined ? '' : value}
                onChange={event => onChange(createPatchFrom(event.target.value))}
                ref={element => this._inputElement = element}
              />
            </FormField>
            <input
              type="text"
              onChange={event => search(event.target.value)}
            />
            {Object.values(ReactIcons).map(X =>{
              return (
                <>
              <X/>
              </>
              );
            })}
            {console.log(ReactIcons.Fa500Px)}
            <ReactIcons.Fa500Px/>
            <Example/>
          </>
        )
    }
}

export default IconPickerCustom;
javascript reactjs search sanity use-state
1个回答
0
投票

React具有两种类型的组件,类组件和功能组件(以前是无状态组件)。当您意识到需要在功能组件内部进行状态并且不希望将其转换为类组件时,可以在功能组件中使用挂钩。

useState()挂钩允许我们在函数组件中添加状态。

类组件

//initialize state
constructor(props) {
    super(props);
    this.state = {foo: bar};
}

//set state
this.setState({foo: baz});

//read state
this.state.foo;

功能组件(带有useState()挂钩)

//initialize state
const [icon, setIcon] = useState("myInitialValue");

//set state
setIcon("myNewValue");

//read state
{icon}

话虽如此,您已经有了一个类组件,所以在这里无需使用钩子。

class IconPickerCustom extends React.Component{

constructor(props) {
    super(props);
    this.state = { icon: "nothing" };
}

static propTypes = {
    value: PropTypes.string,
    onChange: PropTypes.func.isRequired
};

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

const search = value => {
    Object.keys(ReactIcons).map(go => 
        ({ go.toLowerCase().includes(value) ? 
           this.setState({icon:go}) : null;}) 
}

render = () =>{
    const {type, value, onChange} = this.props
    return (
      <>
        <FormField label={type.title} description={type.description}>
          <input
            type="text"
            value={value === undefined ? '' : value}
            onChange={event => onChange(createPatchFrom(event.target.value))}
            ref={element => this._inputElement = element}
          />
        </FormField>
        <input
          type="text"
          onChange={event => search(event.target.value)}
        />
        {Object.values(ReactIcons).map(X =>{
          return (
            <>
          <X/>
          </>
          );
        })}
        {console.log(ReactIcons.Fa500Px)}
        <ReactIcons.Fa500Px/>
        <Example/>
      </>
    )
}
}

export default IconPickerCustom;

您将要遵循这些原则。如果您有任何问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.