Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Discussion options

Problemas com TypeScript

Enquanto usava o select do react-select tive alguns problemas com o valor que não estava sendo capturado e também com as tipagens que não estavam corretas. Então eu retirei a tipagem das Props e da SelectedProps e percebi que ainda assim não conseguia capturar o valor, ai debuguei um pouco o código e cheguei a conclusão que o problema parece estar no momento de acessar o valor, que deveria ser: ref.state.ariaSelection.value e não ref.state.value. Alguém teve um problema similar e achou outra solução?

Exemplo da solução que eu achei:

import  React, { useRef, useEffect } from  "react";

import  ReactSelect  from  "react-select";

import { useField } from  "@unform/core";

type  Option = {

  value: string;
  
  label: string;

};

const  options: Option[] = [

	{ value: "chocolate", label: "Chocolate" },

	{ value: "strawberry", label: "Strawberry" },

	{ value: "vanilla", label: "Vanilla" },

];

export  default  function  Select({ name, ...rest }: any) {

const  selectRef = useRef(null);

const { fieldName, defaultValue, registerField, error } = useField(name);

useEffect(() => {

	registerField({

		name: fieldName,

		ref: selectRef.current,

		getValue: (ref: any) => {

			if (rest.isMulti) {

				if (!ref.state.ariaSelection.value) {

					return [];

				}

				return  ref.state.ariaSelection.value.map(
					(option: Option) =>  option.value);
			}

				if (!ref.state.ariaSelection.value) {

					return  "";

				}

				return  ref.state.ariaSelection.value;

				},

	});

}, [fieldName, registerField, rest.isMulti]);

  

return (

	<ReactSelect

	defaultValue={defaultValue}

	ref={selectRef}

	options={options}

	classNamePrefix="react-select"

	{...rest} />

	);

}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.