从其他组件传递数据到ReactJs组件。

huangapple go评论56阅读模式
英文:

Passing data from other component on ReactJs

问题

I'll provide the translation for the code portions as requested:

这是我的Content组件:

import { GetUrban } from "../Api";
import { useState } from "react";

const Content = () => {
  const [urbanList, setUrban] = useState([]);
  const [inputValue, setInputValue] = useState("");

  const submitValue = () => {
    GetUrban(inputValue).then((result) => {
      setUrban(result);
    });
  };

  return (
    <div>
      <div className="headerContainer parent">
        <div className="searchBar">
          <input
            placeholder="在这里输入内容"
            className="input"
            name="text"
            type="text"
            value={inputValue}
            onChange={(e) => setInputValue(e.target.value)}
          ></input>
          <div className="urbanBar">
            <button className="cta" type="submit">
              <span className="hover-underline-animation" onClick={submitValue}>
                {" "}
                在这里搜索Urban词典
              </span>
            </button>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Content;

这是我的Api组件:

import axios from "axios";

const optionsUrban = {
  method: 'GET',
  url: 'https://mashape-community-urban-dictionary.p.rapidapi.com/define',
  params: {},
  headers: {
    'X-RapidAPI-Key': '在这里输入你的API密钥',
    'X-RapidAPI-Host': 'mashape-community-urban-dictionary.p.rapidapi.com'
  }
};

export async function GetUrban(term) {
  optionsUrban.params.term = term; // 在这里设置参数的值

  const api2 = await axios.request(optionsUrban);
  return api2.data;
}

请注意,我在Content组件的代码中做了一些修改,以便在调用GetUrban函数时传递用户的输入值(inputValue)作为参数。这样,你可以根据用户的输入来获取相应的Urban词典数据。

至于你的问题是否应该将Api组件作为Content组件的子组件,这取决于你的项目结构和需求。通常,如果Api组件可以在多个地方重复使用,将其作为Content组件的子组件是一个好主意。如果你不将其作为子组件,也可以通过直接导入并调用GetUrban函数来获取数据,这取决于你的项目设计。

如果你有更多问题或需要进一步的帮助,请随时提问。

英文:

im new on javascript, especially React.

so i made a page using API that need user to input for "search". How can i pass the "user input" from the Content component ? here's my code. The Api component need "user input" on params.term

Please, explain with the simpler way, because im new here. Thanks in Advance ^^

This is my Content Component

import { GetUrban } from &quot;../Api&quot;;
import { useState } from &quot;react&quot;;

const Content = () =&gt; {
  const [urbanList, setUrban] = useState([]);
  const [inputValue, setInputValue] = useState([]);

  const submitValue = () =&gt; {
    setInputValue(inputValue);
    GetUrban().then((result) =&gt; {
      setUrban(result);
    });
  };

  return (
    &lt;div&gt;
      &lt;div className=&quot;headerContainer parent&quot;&gt;
        
        &lt;div className=&quot;searchBar&quot;&gt;
          &lt;input
            placeholder=&quot;Put something here&quot;
            className=&quot;input&quot;
            name=&quot;text&quot;
            type=&quot;text&quot;
            onChange={((e) =&gt; setInputValue(e.target.value))}
          &gt;&lt;/input&gt;
          &lt;div className=&quot;urbanBar&quot;&gt;
            &lt;button className=&quot;cta&quot; type=&quot;submit&quot;&gt;
              &lt;span className=&quot;hover-underline-animation&quot;
              onClick={submitValue}&gt;
                {&quot; &quot;}
                SEARCH FOR URBAN DICTIONARY HERE{&quot; &quot;}
              &lt;/span&gt;
            &lt;/button&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default Content;

This is my Api Component

import axios from &quot;axios&quot;;

const optionsUrban = {
  method: &#39;GET&#39;,
  url: &#39;https://mashape-community-urban-dictionary.p.rapidapi.com/define&#39;,
  params: { term: {HOW CAN I PASS THE DATA HERE?} },
  headers: {
    &#39;X-RapidAPI-Key&#39;: &#39;API KEY HERE&#39;,
    &#39;X-RapidAPI-Host&#39;: &#39;mashape-community-urban-dictionary.p.rapidapi.com&#39;
  }
};

export async function GetUrban() {
  const api2 = await axios.request(optionsUrban);
  return api2.data;
}

I already make it with single component and it works. But my question is what if i made it separately, the Api component and page component.
i also tried to take my "input data" manually using console.log, and its work. (but im not showing on this code)

Does that mean my Api component being the child of my Content component?
and Should i import using <GetUrban inputData={inputData}/>? kinda confused with it, because when im not import it using that, i still getting the data (if i put the params.term manually)

答案1

得分: 0

以下是您的代码部分的中文翻译:

import { GetUrban } from "../Api";
import { useState } from "react";

const Content = () => {
  const [urbanList, setUrban] = useState([]);
  const [inputValue, setInputValue] = useState([]);

  const submitValue = () => {
    GetUrban(inputValue).then((result) => {
      console.log(result);
      setUrban(result);
    });
  };

  return (
    <div>
      <div className="headerContainer parent">
        <div className="searchBar">
          <input
            placeholder="在这里输入内容"
            className="input"
            name="text"
            type="text"
            onChange={(e) => setInputValue(e.target.value)}
          ></input>
          <div className="urbanBar">
            <button className="cta" type="submit">
              <span className="hover-underline-animation" onClick={submitValue}>
                {" "}
                在这里搜索 Urban Dictionary{" "}
              </span>
            </button>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Content;
import axios from "axios";

export async function GetUrban(inputValue) {
  const api2 = await axios.get('https://mashape-community-urban-dictionary.p.rapidapi.com/define', {
    params: {
      term: inputValue
    },
    headers: {
      'X-RapidAPI-Key': '在此输入您的API密钥',
      'X-RapidAPI-Host': 'mashape-community-urban-dictionary.p.rapidapi.com'
    }
  });
  return api2.data;
}
英文:

Your Content component:

import { GetUrban } from &quot;../Api&quot;;
import { useState } from &quot;react&quot;;

const Content = () =&gt; {
  const [urbanList, setUrban] = useState([]);
  const [inputValue, setInputValue] = useState([]);
   
  const submitValue = () =&gt; {
    GetUrban(inputValue).then((result) =&gt; {
      console.log(result)
      setUrban(result);
    });
  };

  return (
    &lt;div&gt;
      &lt;div className=&quot;headerContainer parent&quot;&gt;
        
        &lt;div className=&quot;searchBar&quot;&gt;
          &lt;input
            placeholder=&quot;Put something here&quot;
            className=&quot;input&quot;
            name=&quot;text&quot;
            type=&quot;text&quot;
            onChange={((e) =&gt; setInputValue(e.target.value))}
          &gt;&lt;/input&gt;
          &lt;div className=&quot;urbanBar&quot;&gt;
            &lt;button className=&quot;cta&quot; type=&quot;submit&quot;&gt;
              &lt;span className=&quot;hover-underline-animation&quot;
              onClick={submitValue}&gt;
                {&quot; &quot;}
                SEARCH FOR URBAN DICTIONARY HERE{&quot; &quot;}
              &lt;/span&gt;
            &lt;/button&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default Content;

Your GetUrban function:

import axios from &quot;axios&quot;;
            
   export async function GetUrban(inputValue) {
     const api2 = await axios.get(&#39;https://mashape-community-urban- 
       dictionary.p.rapidapi.com/define&#39;, {
          params: {
          term: inputValue
          },
          headers: {
          &#39;X-RapidAPI-Key&#39;: &#39;API KEY HERE&#39;,
          &#39;X-RapidAPI-Host&#39;: &#39;mashape-community-urban-dictionary.p.rapidapi.com&#39;
        }
        });
        return api2.data;
    }

huangapple
  • 本文由 发表于 2023年1月9日 11:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定