当 props 改变时渲染组件,否则不显示

我有这个 Output 组件,它应该在从父组件获取道具时呈现。


我怎样才能做到这一点?现在,当我单击一个按钮但没有调用 getTranslate 函数时它会呈现。在调试器中,我可以看到该组件具有所需的道具,它只是不运行我的 getTranslate 函数


import React, { useState } from "react";


import Axios from "axios";


const Output = (props) => {

  const [output, setOutput] = useState(null);

  const [url, setUrl] = useState(null);


  const getUrl = (language, text) => {

    console.log("GetURL ran with: ", language, text);

    let url = "";

    switch (language) {

      case "yoda":

        url =

          " https://api.funtranslations.com/translate/yoda.json?text=" + text;

        break;


      case "valyrian":

        url =

          "https://api.funtranslations.com/translate/valyrian.json?text=" +

          text;

        break;


      case "sith":

        url =

          "https://api.funtranslations.com/translate/sith.json?text=" + text;

        break;


      case "shakespeare":

        url =

          "https://api.funtranslations.com/translate/shakespeare.json?text=" +

          text;

        break;


      case "pirate":

        url =

          "https://api.funtranslations.com/translate/pirate.json?text=" + text;

        break;


      case "minion":

        url =

          "https://api.funtranslations.com/translate/minion.json?text=" + text;

        break;


      case "lolcat":

        url =

          "https://api.funtranslations.com/translate/lolcat.json?text=" + text;

        break;


      case "klingon":

        url =

          "https://api.funtranslations.com/translate/klingon.json?text=" + text;

        break;


      case "hacker":

        url =

          "https://api.funtranslations.com/translate/hacker.json?text=" + text;

        break;


      case "dothraki":

        url =

          "https://api.funtranslations.com/translate/dothraki.json?text=" +

          text;

        break;


      default:

        break;

    }


    setUrl(url);

  };



慕哥9229398
浏览 128回答 4
4回答

12345678_0001

可能的问题是你没有调用 getTranslate 函数,看起来应该是 getTranslate(); 您还需要在收到数据后更新状态。请参阅我已放置评论以指导解决方案... getTranslate 未返回任何值,并且您调用它的地方不合适...const getTranslate = () => {&nbsp; &nbsp; getUrl(props.language, props.text);&nbsp; &nbsp; const axios = require("axios");&nbsp; &nbsp; axios.get(function (respone) {&nbsp; &nbsp; &nbsp; console.log("Here comes response from api call: ", respone);/// update the variable /state/ prop that you want to bind&nbsp; &nbsp; &nbsp; return respone.contents.translated;&nbsp; &nbsp; });&nbsp; };然后绑定模板中的值:return (&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; <textarea placeholder="Translated..." value={ the value to bind from state/prop}></textarea>&nbsp; &nbsp; </div>&nbsp; );

蝴蝶刀刀

要调用 getTranslate(),您可以使用钩子生命周期方法 useEffect()。从该函数返回的值可以设置为某种状态并将设置的状态绑定到文本区域值。import React, { useState, useEffect } from "react";import Axios from "axios";const Output = (props) => {&nbsp; const [output, setOutput] = useState('');&nbsp; const [url, setUrl] = useState('');&nbsp; useEffect(()=>{&nbsp; &nbsp; getTranslate();&nbsp; })&nbsp; const getUrl = (language, text) => {&nbsp; &nbsp; console.log("GetURL ran with: ", language, text);&nbsp; &nbsp; let url = "";&nbsp; &nbsp; switch (language) {&nbsp; &nbsp; &nbsp; case "yoda":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " https://api.funtranslations.com/translate/yoda.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "valyrian":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/valyrian.json?text=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "sith":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/sith.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "shakespeare":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/shakespeare.json?text=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "pirate":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/pirate.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "minion":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/minion.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "lolcat":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/lolcat.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "klingon":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/klingon.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "hacker":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/hacker.json?text=" + text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; case "dothraki":&nbsp; &nbsp; &nbsp; &nbsp; url =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.funtranslations.com/translate/dothraki.json?text=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; setUrl(url);&nbsp; };&nbsp; const getTranslate = () => {&nbsp; &nbsp; console.log("From get translate")&nbsp; &nbsp; getUrl(props.language, props.text);&nbsp; &nbsp; Axios.get(function (respone) {&nbsp; &nbsp; &nbsp; console.log("Here comes response from api call: ", respone);&nbsp; &nbsp; &nbsp;setOutput(respone.contents.translated);&nbsp; &nbsp; });&nbsp; };&nbsp; return (&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; <textarea placeholder="Translated..." value={output} onChange={()=>console.log("Testing")}></textarea>&nbsp; &nbsp; </div>&nbsp; );};export default Output;

陪伴而非守候

您需要将翻译功能移动到“onChange”功能或“onBlur”功能(如果您希望在用户点击后翻译文本)。此外,您传递给 onChange 和 onBlur 的函数应该管理翻译功能的异步性质。

慕尼黑的夜晚无繁华

您可以使用 useEffect 方法,该方法将在每次道具更改或组件渲染时运行。useEffect(()=>{getTranslate();&nbsp;}, [props])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript