Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

reactjs - Argument of type 'IAulasAdicionais[]' is not assignable to parameter of type 'string'

everyone i have one problem to solve into my application but i don't know how i can do this i hope someone else help me, my problem is one error into my code, in bellow of my code have some prints of this issue,

Mock data:

"AulasAdicionais": [
    {
      "Periodo": "1",
      "HorarioInicio": "2021-01-19T07:30:00-0300",
      "HorarioTermino": "2021-01-19T08:30:00-0300",
      "IdDisciplina": "396",
      "DescricaoDisciplina": "Matemática",
      "DescricaoReduzidaDisciplina": "Mat",
      "DescricaoTurno": "Manh?",
      "IdEstabelecimento": "43",
      "DescricaoEstabelecimento": "Colégio Correio Lima",
      "TipoSituacaoHorario": "Normal"
    }
  ]

Service

export interface IAulasAdicionais {
  Periodo: string;
  HorarioInicio: string;
  HorarioTermino: string;
  IdDisciplina: string;
  DescricaoDisciplina: string;
  DescricaoReduzidaDisciplina: string;
  DescricaoTurno: string;
  IdEstabelecimento: string;
  DescricaoEstabelecimento: string;
  TipoSituacaoHorario: string;
}

export interface IAgendaAulas {  
  AulasAdicionais: IAulasAdicionais[];

}

const getAgendaAulas = async (dataAula: string ) : Promise<IAgendaAulas[] | undefined> => {
  try {
    const { data } = await Api().get<IAgendaAulas[]>(
      `/agenda-aulas?DataAula=${dataAula}`
    );
    if (data) {
      return data;
    } else {
      return undefined;
    }
  } catch (error) {
    return undefined;
  }
};

TSX:

interface IAgendasAulasProps {
  aulasAdicionais: IAulasAdicionais[];
}

export const AulasAdicionaisRecuperacao: React.FC<IAgendasAulasProps> = ({
  aulasAdicionais,
}) => {
  const classes = useStyles();
  const [aulaAdicionais, setAulasAdicionais] = useState<
    IAulasAdicionais[]
  >();

  useEffect(() => {
    AgendaTurmaService.getAgendaAulas(aulasAdicionais "O problema está aqui" ).then((data) => {
      if (data) {
        setAulasAdicionais(data "e aqui");
      } else {
        setAulasAdicionais([]);
      }
    });
  }, [aulasAdicionais]);

{aulaAdicionais?.map((horario, horarioIndex) => (
        <>
          <Grid container spacing={2} key={horarioIndex}>
            <Grid item lg={2}>
              <Box margin={1}>
                <Paper className={classes.root}>
                  <Box marginTop={2} marginLeft={1}>
                    <ClassIcon color="primary" style={{ fontSize: 90 }} />
                    <Box display="flex" marginTop={-10} marginLeft={12}>
                      <Typography>{horario?.DescricaoDisciplina}</Typography>
                    </Box>
                  </Box>
                </Paper>
              </Box>
            </Grid>
          </Grid>
        </>
      ))}

Here is a prints of my issues, i try many things but din't work and I didn't find anything in web if solve my problem ! Erro 1

Erro 2

question from:https://stackoverflow.com/questions/65890345/argument-of-type-iaulasadicionais-is-not-assignable-to-parameter-of-type-st

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Be careful to set the proper type to useState. When you set aulaAdicionais you're using IAgendaAulas instead of IAulasAdicionais.

Try these changes:

const getAgendaAulas = async (dataAula: string ) : Promise<IAgendaAulas | undefined> => {

setAulasAdicionais(data.AulasAdicionais);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...