Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在React JS中如何使用函数的变量作为道具 import React,{useState}来自“React” 从“./components/Display”导入显示; 常量应用=()=>{ const[input,setInput]=useState(“”); const getData=async()=>{ const myAPI=等待获取(`http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761`); const response=wait myAPI.json(); console.log(response);//要将response用作显示组件中的道具吗 } 返回( 天气预报 //这里 //这里 //这里 setInput(e.target.value)}className=“输入”/> 取来 ); } 导出默认应用程序;_Reactjs_Async Await_Fetch Api_React Props - Fatal编程技术网

Reactjs 在React JS中如何使用函数的变量作为道具 import React,{useState}来自“React” 从“./components/Display”导入显示; 常量应用=()=>{ const[input,setInput]=useState(“”); const getData=async()=>{ const myAPI=等待获取(`http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761`); const response=wait myAPI.json(); console.log(response);//要将response用作显示组件中的道具吗 } 返回( 天气预报 //这里 //这里 //这里 setInput(e.target.value)}className=“输入”/> 取来 ); } 导出默认应用程序;

Reactjs 在React JS中如何使用函数的变量作为道具 import React,{useState}来自“React” 从“./components/Display”导入显示; 常量应用=()=>{ const[input,setInput]=useState(“”); const getData=async()=>{ const myAPI=等待获取(`http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761`); const response=wait myAPI.json(); console.log(response);//要将response用作显示组件中的道具吗 } 返回( 天气预报 //这里 //这里 //这里 setInput(e.target.value)}className=“输入”/> 取来 ); } 导出默认应用程序;,reactjs,async-await,fetch-api,react-props,Reactjs,Async Await,Fetch Api,React Props,我不知道我是否正确理解您,但如果我是对的,您希望访问从API获取的函数返回的数据,如果是,您可以尝试这种方法 import React, { useState } from 'react' import Display from './components/Display'; const App = () => { const [input,setInput] = useState(""); const getData = async () =

我不知道我是否正确理解您,但如果我是对的,您希望访问从API获取的函数返回的数据,如果是,您可以尝试这种方法

import React, { useState } from 'react'
import Display from './components/Display';
const App = () => {
    const [input,setInput] = useState("");
    
    const getData = async () => {
    const myAPI = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761`);
    const response = await myAPI.json();
    console.log(response);                  //want to use response as a prop in Display component
   }

   return(
    <div className="container">
        <h1>Weather Report</h1>
        <Display title={"City Name :"} />         //here
        <Display title={"Temperature :"} />       //here
        <Display title={"Description :"} />       //here
        <input type={input} onChange={e => setInput(e.target.value)} className="input"/>
        <button className="btn-style" onClick={getData}>Fetch</button>
    </div>
   );
}

export default App;
import React,{useState,useffect}来自“React”
从“./components/Display”导入显示;
从“axios”导入axios;
常量应用=()=>{
const[input,setInput]=useState(“”);
const[state,setState]=useState({loading:true,fetchedData:null});
useffect(()=>{
getData();
},[setState]);
异步函数getData(){
setState({loading:true});
康斯特阿皮乌尔酒店http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761';
等待axios.get(apirl)。然后((回购)=>{
常数rData=repos.data;
setState({loading:false,fetchedData:rData});
});
}
返回(
state.loading?:(
{state.fetchedData.map((行)=>)
天气预报
//这里
//这里
//这里
)) }
)
);

}

您可以在API响应中使用另一个useState。像
const[response,setResponse]=useState({})
,然后在getData中调用
setResponse(response)
,并将响应传递给显示组件请解释您试图实现什么,以及您指的是哪个函数?@GouthamJ.M我只想传递响应(object)您导入的axios是什么?这只是一个包,可以轻松地向REST端点发送异步HTTP请求并执行CRUD操作
import React, { useState, useEffect } from 'react' 
import Display from './components/Display';
import axios from 'axios';

const App = () => {
const [input,setInput] = useState(""); 

const [state, setState] = useState({loading: true, fetchedData: null});

useEffect(() => {
        getData();
}, [setState]);

async function getData() {
    setState({ loading: true });
    const apiUrl = 'http://api.openweathermap.org/data/2.5/weather?q=${input}&units=metric&appid=60dfee3eb8199cac3e55af5339fd0761';
    await axios.get(apiUrl).then((repos) => {
        const rData = repos.data;
        setState({ loading: false, fetchedData: rData });
    });
}

return(
    state.loading ? <CircularProgress /> : ( 
        <List className={classes.root}>
        { state.fetchedData.map((row) => ( 
            <div className="container">
                <h1>Weather Report</h1>
                <Display title={"City Name :" + row.cityName } />         //here
                <Display title={"Temperature :" + row.temperature} />       //here
                <Display title={"Description :" + row.description} />       //here
                 
            </div>
        )) }
        </List>
    )
);