gpt4 book ai didi

javascript - 在 react 中将状态传递给另一个文件中的组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:13 24 4
gpt4 key购买 nike

我有一个 App 组件,其中包含一些组件,例如菜单、侧边栏、存储库等。在菜单组件中有一个搜索栏。当您提交在搜索栏中输入的任何内容时,它会向 https://api.github.com/users/ 发出请求。 .然后我将 json 更改为用户状态。但是如何将数据传递给 repos 组件呢?这可能吗?

App.js

import React, { Component } from 'react';
import Sidebar from './layout/Sidebar';
import Menu from './layout/Menu';
import Repos from './githubdata/Repos';
import Followers from "./githubdata/Followers";
import Following from "./githubdata/Following";
import BirthCount from "./githubdata/BirthCount";

class App extends Component{
render(){
return(
<div className="wrapper">
<Sidebar />
<div className="main-panel">
{* In this component the magic happens *}
<Menu />
<div className="content">
<div className="container-fluid">
<div className="row">
{* I want to display the data in this component *}
<Repos />
<Followers />
<Following />
<BirthCount />
</div>
</div>
</div>
</div>
</div>
)
}
}

export default App;

Menu.js

import React, { Component } from 'react';

class Menu extends Component {
constructor(props){
super(props);
this.state = {
query: ''
}
}

handleChange = (event) => {
this.setState({
query: event.target.value
})
};

search = () => {
const BASE_URL = 'https://api.github.com/users/';
let FETCH_URL = `${BASE_URL}${this.state.query}`;
console.log('fetchurl', FETCH_URL)

fetch(FETCH_URL, {
method: 'GET'
})
.then(response => response.json())
.then(json => {
const user = json;
console.log('user json', user);
this.setState({
user
})
});
};

render(){
return(
<div>
<input onChange={this.handleChange} value=this.state.query} type="text"/>
<button onClick={this.search}>submit</button>
</div>
)
}
}
export default Menu;

最佳答案

将您的搜索功能移动到父组件 App

在Repo中你可以通过this.props.user获取用户

App.js

import React, { Component } from "react";
import Sidebar from "./layout/Sidebar";
import Menu from "./layout/Menu";
import Repos from "./githubdata/Repos";
import Followers from "./githubdata/Followers";
import Following from "./githubdata/Following";
import BirthCount from "./githubdata/BirthCount";

class App extends Component {
constructor() {
super();
this.state = { user };
}
search = (query) => {
const BASE_URL = "https://api.github.com/users/";
let FETCH_URL = `${BASE_URL}${query}`;
console.log("fetchurl", FETCH_URL);

fetch(FETCH_URL, {
method: "GET"
})
.then(response => response.json())
.then(json => {
const user = json;
console.log("user json", user);
this.setState({
user
});
});
};

render() {
return (
<div className="wrapper">
<Sidebar />
<div className="main-panel">
{/* In this component the magic happens */}
<Menu search={this.search.bind(this)} />
<div className="content">
<div className="container-fluid">
<div className="row">
{/* I want to display the data in this component */}
<Repos user={this.state.user} />
<Followers />
<Following />
<BirthCount />
</div>
</div>
</div>
</div>
</div>
);
}
}

export default App;

关于javascript - 在 react 中将状态传递给另一个文件中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46950103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com