英文:
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 "../Api";
import { useState } from "react";
const Content = () => {
const [urbanList, setUrban] = useState([]);
const [inputValue, setInputValue] = useState([]);
const submitValue = () => {
setInputValue(inputValue);
GetUrban().then((result) => {
setUrban(result);
});
};
return (
<div>
<div className="headerContainer parent">
<div className="searchBar">
<input
placeholder="Put something here"
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}>
{" "}
SEARCH FOR URBAN DICTIONARY HERE{" "}
</span>
</button>
</div>
</div>
</div>
</div>
);
};
export default Content;
This is my Api Component
import axios from "axios";
const optionsUrban = {
method: 'GET',
url: 'https://mashape-community-urban-dictionary.p.rapidapi.com/define',
params: { term: {HOW CAN I PASS THE DATA HERE?} },
headers: {
'X-RapidAPI-Key': 'API KEY HERE',
'X-RapidAPI-Host': 'mashape-community-urban-dictionary.p.rapidapi.com'
}
};
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 "../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="Put something here"
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}>
{" "}
SEARCH FOR URBAN DICTIONARY HERE{" "}
</span>
</button>
</div>
</div>
</div>
</div>
);
};
export default Content;
Your GetUrban function:
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 KEY HERE',
'X-RapidAPI-Host': 'mashape-community-urban-dictionary.p.rapidapi.com'
}
});
return api2.data;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论