简体   繁体   English

将箭头 function 传递给组件 prop

[英]pass arrow function to component prop

I am trying to pass the result of the handleRedirectUrl() function to the ShortUrlField component as a prop.我正在尝试将 handleRedirectUrl() function 的结果作为道具传递给 ShortUrlField 组件。

I don't know what I am doing wrong, please help me我不知道我在做什么错,请帮助我

const handleRedirectUrl = () => {
    urlService
      .getShortenedUrl(urls.slice(-1)[0].short_url)
      .then((returnedUrl) => {
        setRedirectedUrl(returnedUrl);
      })
      .catch((error) => {
        handleCreateErrors(error);
      })
      .finally(() => {
        return redirectedUrl;
      });
  };

  //display shortened url
  const shortUrlDisplay = renderShortUrl ? (
    <ShortUrlField
      originalUrlValue={urls.slice(-1)[0].original_url}
      shortUrlValue={urls.slice(-1)[0].short_url}
      redirectedUrlValue={handleRedirectUrl()}
    />
  ) : (
    <EmptyField />
  );

The urlService function urlService function

const getShortenedUrl = (urlToGet) => {
  const request = axios.get(redirectShortenedUrl + `${urlToGet}`);
  return request.then((response) => response.data);
};

Edit 1: I was not returning anything with my handleRedirectUrl function.编辑1:我的handleRedirectUrl function没有返回任何东西。 Also, I was not passing it properly to the props.另外,我没有正确地将它传递给道具。 I have changed my code to我已将代码更改为

  const handleRedirectUrl = () => {
  return urlService
    .getShortenedUrl(urls.slice(-1)[0].short_url)
    .then((returnedUrl) => {
      setRedirectedUrl(returnedUrl);
    })
    .catch((error) => {
      handleCreateErrors(error);
    })
    .finally(() => {
      return redirectedUrl;
    });
};
//display shortened url
const shortUrlDisplay = renderShortUrl ? (
  <ShortUrlField
    originalUrlValue={urls.slice(-1)[0].original_url}
    shortUrlValue={urls.slice(-1)[0].short_url}
    redirectedUrlValue={handleRedirectUrl}
  />
) : (
  <EmptyField />
);

It does not work.这没用。 the getShortenedUrl function is never called永远不会调用getShortenedUrl function

Edit 2: Added the ShortUrlField component code编辑 2:添加了ShortUrlField组件代码

import React from "react";


const ShortUrlField = (props) => {
return (
  <div>
    <p>
      <a href={props.originalUrlValue}>{props.originalUrlValue}</a> became{" "}
      <a href={props.redirectUrlValue}>{props.shortUrlValue}</a>
    </p>
  </div>
);
};

export default ShortUrlField;

Edit 3: I made it work!!编辑3:我成功了!!

Many thanks to @ZsoltMeszaros for pointing out the right path to me.非常感谢@ZsoltMeszaros为我指出了正确的道路。

I have passed a state variable to my conditional rendered component, and added an effect hook that basically sets the state if the component is rendered.我已经将一个 state 变量传递给我的条件渲染组件,并添加了一个效果挂钩,如果组件被渲染,它基本上会设置 state。

Much thanks to all of you that commented.非常感谢所有评论的人。

I didn't understand where is this setRedirectURL function, but anyway your handleRedirectUrl function returns nothing我不明白这个setRedirectURL function 在哪里,但无论如何你的handleRedirectUrl function 什么也没返回

const handleRedirectUrl = () => {
  return urlService
    .getShortenedUrl(urls.slice(-1)[0].short_url)
    .then((returnedUrl) => {
      setRedirectedUrl(returnedUrl);
    })
    .catch((error) => {
      handleCreateErrors(error);
    })
    .finally(() => {
      return redirectedUrl;
    });
};

May this can work as you can see like your axios request, this is returning result of urlService.可能这可以像您看到的 axios 请求一样工作,这是 urlService 的返回结果。

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

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