What is the correct way to input the 'name' HTML attribute in an Ant Design select element?

I am facing an issue with the 'name' attribute in my Ant Design Select component. When trying to set the 'name' attribute, I encountered an error message that is causing issues.

https://i.stack.imgur.com/Lzb4t.png

Ant Design Select Component

interface Props {
  name?: string;
  value?: string;

}
export const Select = ({ name, ...props }: Props) => {
  const { Option } = SelecteAntd;

     import {Select } from 'antd';
        
                   <Select
                      bordered={false}
                      {...props}
                      name={name}
                    >
                      <Option value="Youth">under 24</Option>
                      <Option value="Adult">25 - 64</Option>
                      <Option value="Senior">64+</Option>
            </Select>

Form Component

import { Select } from '../shared/Select';

                 <Select 
                        value={surveyData.age}
                        name='age' <------
                        onChange={handleChange}
                    />

Answer №1

I found a solution that works well for me.

import React from "react";
import { useState } from "react";


const App =()=>{
  const [selectedState, setSelectedState] = useState(')

const handleInputChange =(selectedData,option)=>{
    console.log("[selected Data] ",selectedData);
    console.log("[selected option]",option);
   // setSelectedState(date.name)
  }
 // Make sure to use antd select
return (<Select value={selectedState} onChange={handleChange}>
      <Option name="state" value="jack">Jack</Option>
      <Option name="state" value="lucy">Lucy</Option>
      <Option value="Yiminghe">yiminghe</Option>
    </Select>)    
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

To Handle Dynamic Data

import { Select } from 'antd';
import React from 'react';
const { Option } = Select;

const DropdownComponent=({errMsg,optionList,...props})=>{
return <div>
   <Select   style={{ width: 120 }} {...props}>
 {optionList.map(({state,name})=>{
    return <Option name={props.name}  key={state} value=. 
 {state}>{name}</Option>
 })}
</Select>
{errMsg&&<p>{errMsg}</p>}
</div>;
};
export default DropdownComponent;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

React Component Div Containing a Hydration Error

Can someone help me resolve the Hydration error related to a nested div issue? I am working on a component that has two main functions - fetching data and mapping it. However, I keep encountering a hydration error and I'm not sure why it's happe ...

I am encountering an issue where the object I am sending with Next.js is appearing as undefined on the receiving page

When transferring data from my question screen to a result screen, I am using an object called footprintsData. Here is the code snippet for sending the data: const footprintsData = { carFootprint: roundedCarFootprint, electricityFootprint: roundedElect ...

Guide to setting headers to application/json in Angular 2

I've been attempting to send an HTTP post request in Angular 2, but I'm facing difficulties setting the headers to content type application JSON. This is my current code: login(url, postdata) { var headers = new Headers({'Content-Type&a ...

How to resolve undefined callback when passing it to a custom hook in React Native

I'm facing an issue with passing a callback to my custom hook: export default function useScreenshotDetection(onScreenshot) { useEffect(() => { ... onScreenshot(); ... }, []); } Strangely, the callback is not being detected ...

Experiencing difficulty in finding my way to other web pages

I've developed a React website with user authentication and a sidebar. The routes are protected to ensure that only logged-in users can access certain pages. To show the sidebar only after login, I have placed it inside protected routes and kept sign- ...

Transitioning Apollo Server from version 3 to version 4 within a next.js environment

Previously in v3, you could define "createHandler" like this: export default async (req, res) => { await startServer; await apolloServer.createHandler({ path: "/api/graphql", })(req, res); }; However, in v4, this is no longer possi ...

Troubleshooting the issue of Algolia-Sanity updates not functioning properly in Next.js

I've been attempting to automate the updating of my Algolia index by utilizing the sanity-algolia package along with the sanity webhook (specifically the legacy webhook), but unfortunately, it's not functioning as expected. Below is a snippet fro ...

Obtain the unique session identification code from the React application

Currently, I am diving into learning Express.js and exploring how to build an API while connecting it with a React frontend. During this process in Express, one of the key components I am utilizing is express-session for managing sessions. Additionally, fo ...

Tips on integrating the createjs library into a ReactJS project

Hey there! I'm currently working on developing a canvas-based app using ReactJS, and I need to integrate the CreateJS library. As a newcomer to ReactJS, I've been struggling to figure out the best approach. I've tried two different methods - ...

Angular 2+ encountering an internal server error (500) while executing an http.post request

Here is my service function: public postDetails(Details): Observable<any> { let cpHeaders = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: cpHeaders }); return this.htt ...

I am struggling to display the data fetched by Next.js on the page

I am struggling to display the data from the first file in my tanstack table or as a string within the HTML, even though I can see it in a console.log when grabbed by axios. The tanstack table worked fine with hardcoded data. In the console image provided, ...

What is the best way to implement Angular translation for multiple values in a typescript file, while also incorporating parameters?

this.snackBar.open( `Only files of size less than ${this.fileSizeAllowed}KB are allowed`, this.translate.instant('USER_REG.close'), { panelClass: 'errorSnackbar', ...

Typescript - unexpected behavior when using imported JavaScript types:

I am struggling with headaches trying to integrate an automatically generated JavaScript library into TypeScript... I have packaged the JavaScript library and d.ts file into an npm package, installed the npm package, and the typings modules in the TypeScr ...

The React project encountered a build failure following the execution of the npm run command, resulting in the error code ELIFCY

I encountered an error while attempting to run my React project on localhost. Is there anyone who can provide assistance? Here is the error log: C:\Users\Intel\Desktop\burger-project>npm start > <a href="/cdn-cgi/l/email-prote ...

useEffect runs endlessly

Currently, I am using React with hooks to handle API calls and implement autoscroll functionality on a data-heavy screen. However, I have encountered a problem where the autoscroll feature implemented through a separate useEffect is interfering with the ot ...

Trouble creating Blog pages with Next.js and Sanity

I am currently in the process of integrating Sanity with Next.Js for the first time, as I want to add a blog section to my personal website. Everything seems to be working fine during development, but when I try to deploy or build the project, an error occ ...

Having trouble navigating through multiple layers of nested array data in react js

I need help understanding how to efficiently map multiple nested arrays of data in a React component and then display them in a table. The table should present the following details from each collection: title, location, description, and keywords. Below ...

Turning XSD into TypeScript code

Stumbling upon this tool called CXSD, I was intrigued. The documentation describes cxsd as a streaming XSD parser and XML parser generator designed for Node.js and TypeScript (optional but highly recommended). It seemed like the perfect solution for my ne ...

Endless Cycle of Promise and useState Hook

I'm struggling with a situation where I unintentionally created an infinite loop using a combination of a fetch promise and a useState hook in React. Could someone shed some light on why this loop keeps repeating? My understanding of both concepts is ...

The React callback is failing to update before navigating to the next page

I'm facing an issue that seems like it can be resolved through the use of async/await, but I am unsure of where to implement it. In my application, there are three components involved. One component acts as a timer and receives a callback from its pa ...