我正在学习反应,看来我无法在类组件中定义功能组件,还是我做错了什么?

发布于 2025-01-21 14:09:00 字数 2473 浏览 0 评论 0 原文

正如标题所说,试图在我的班级中声明一个组件,但是会遇到一个不确定的错误。我不能这样做还是做错了?正如我所说,我是新手反应,因此任何信息都会有所帮助。总体目标是创建一个表单,但我只是尝试根据以前的选项来弹出某些选项。如果代码太多让我知道,我会将其修剪为导入的内容,但我认为我会提供整个事情,因为它不长。

import React, {useState, useMemo} from 'react';
import ReactDOM from 'react-dom/client';
import countryList from 'react-select-country-list';
import './index.css';
//import App from './App';


class StartForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.options = countryList().getData();
  }

  handleChange(event) {
    this.setState({[event.target.name]: event.target.value});
    //alert('Your favorite flavor is: ' + this.state.value);
    console.log(this.state.accType);
  }

  handleSubmit(event) {
    //alert('Your favorite flavor is: ' + this.state.value);
    //event.preventDefault();
  }

//------------------- THIS COMPONENT
  CustomOptions(props) {
    return(
        <label>
          <input type="checkbox" id="acss_debit_payments" name="acss_debit_payments" value={this.value.acss_debit_payments} onChange={this.handleChange} />Paneer
        </label>
      );
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Select Account Type:
          <select name="accType" value={this.state.accType} onChange={this.handleChange}>
            <option value="custom">Custom</option>
            <option value="express">Express</option>
            <option value="standard">Standard</option>
          </select>
        </label>
        <br/>
        <label>
          Select Country Code(Optional):
          <select name="country" options={this.options} value={this.state.country} onChange={this.handleChange}/>
        </label>
        <br/>
        <label>
          Email:
          <input type="text" name="email" value={this.state.email} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />

        {this.state.accType == "custom" &&
          <CustomOptions />
      }

      </form>
    );
  }
}


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <StartForm/>
  </React.StrictMode>
);


As the title says, trying to declare a component within my class, however, getting an Undefined error. Can I not do this or am I doing it wrong? As I said, I'm new to React so any bit of info helps. The overall goal is to create a form but I'm trying to only make certain options pop depending on what the previous options are. If the code is too much let me know, I'll trim it down to what's import but I figured I'd provide the whole thing cause it's not that long.

import React, {useState, useMemo} from 'react';
import ReactDOM from 'react-dom/client';
import countryList from 'react-select-country-list';
import './index.css';
//import App from './App';


class StartForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.options = countryList().getData();
  }

  handleChange(event) {
    this.setState({[event.target.name]: event.target.value});
    //alert('Your favorite flavor is: ' + this.state.value);
    console.log(this.state.accType);
  }

  handleSubmit(event) {
    //alert('Your favorite flavor is: ' + this.state.value);
    //event.preventDefault();
  }

//------------------- THIS COMPONENT
  CustomOptions(props) {
    return(
        <label>
          <input type="checkbox" id="acss_debit_payments" name="acss_debit_payments" value={this.value.acss_debit_payments} onChange={this.handleChange} />Paneer
        </label>
      );
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Select Account Type:
          <select name="accType" value={this.state.accType} onChange={this.handleChange}>
            <option value="custom">Custom</option>
            <option value="express">Express</option>
            <option value="standard">Standard</option>
          </select>
        </label>
        <br/>
        <label>
          Select Country Code(Optional):
          <select name="country" options={this.options} value={this.state.country} onChange={this.handleChange}/>
        </label>
        <br/>
        <label>
          Email:
          <input type="text" name="email" value={this.state.email} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />

        {this.state.accType == "custom" &&
          <CustomOptions />
      }

      </form>
    );
  }
}


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <StartForm/>
  </React.StrictMode>
);


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2025-01-28 14:09:00

要调用自定义您需要使用 this 的当前实例:

<this.CustomOptions />

示例: https://codesandbox.io/s/happy-swartz-hsm6ng?file=%2FSRC%2fapp.js

To call CustomOptions you need to reference the current instance with this:

<this.CustomOptions />

Example: https://codesandbox.io/s/happy-swartz-hsm6ng?file=%2Fsrc%2FApp.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文