简体   繁体   English

如何在 render() 中定义 useState 变量 reactjs

[英]How to define useState variable inside render() reactjs

I am new to reactjs and I am defining useState variables inside render(){} method of reactjs but it is giving me error:Invalid hook call.我是 reactjs 的新手,我在 reactjs 的 render(){} 方法中定义了 useState 变量,但它给我错误:无效的钩子调用。

class ProductDefinition extends Component {
  constructor(props) {
    super(props);
      this.state = {
          onProductCreated: undefined,
    };
  }

    componentDidMount() {
    }
     
    render() {
        const [imageUrl, setImageUrl] = useState(undefined);
        const [loading, setLoading] = useState(false);

    const uploadButton = (
                <div>
                    {loading ? <LoadingOutlined /> : <PlusOutlined />}
                    <div style={{ marginTop: 8 }}>Upload</div>
                </div>
            );


            return(
            <div style={{ padding: '5px', textAlign: 'left' }}>
  <Form.Item
                                    label="Icon / Thumbnail URL:"
                                    name="productUrl"
                                 >

                                    <Upload
                                        name="avatar"
                                        listType="picture-card"
                                        className="avatar-uploader"
                                        showUploadList={false}
                                       
                                    >
                                        {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
                                    </Upload>

                            </Form.Item>
                        </div>)

             }
}

在此处输入图像描述

What are the ways to define imageUrl and loading variable with useState outside render() method?在 render() 方法之外使用 useState 定义 imageUrl 和加载变量的方法有哪些?

useState is the concept of React Hook , and you need to define the useState in the functional component, not class component. useStateReact Hook的概念,你需要在功能组件中定义 useState ,而不是 class 组件。

 function ProductDefinition() { const [imageUrl, setImageUrl] = useState(undefined); const [loading, setLoading] = useState(false); const uploadButton = () => ( <div> {loading? <LoadingOutlined />: <PlusOutlined />} <div style={{ marginTop: 8 }}>Upload</div> </div> ); return ( <div style={{ padding: '5px', textAlign: 'left' }}> <Form.Item label="Icon / Thumbnail URL:" name="productUrl" > <Upload name="avatar" listType="picture-card" className="avatar-uploader" showUploadList={false} > {imageUrl? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} />: uploadButton()} </Upload> </Form.Item> </div> ) }

I think you are get confused between function-based component and class based components.我认为您对基于函数的组件和基于 class 的组件感到困惑。 The hooks are only applicable for function based components.钩子仅适用于基于 function 的组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM