Javascript React:如何从一个组件获取API数据并将其传递给另一个组件
我试图从搜索栏组件传递数据,搜索栏组件从API调用返回JSON,另一个组件将显示信息。我很难弄明白。我尝试制作一个容器来容纳这两个组件并传递数据。任何帮助都将不胜感激 谢谢 我的搜索栏组件Javascript React:如何从一个组件获取API数据并将其传递给另一个组件,javascript,reactjs,Javascript,Reactjs,我试图从搜索栏组件传递数据,搜索栏组件从API调用返回JSON,另一个组件将显示信息。我很难弄明白。我尝试制作一个容器来容纳这两个组件并传递数据。任何帮助都将不胜感激 谢谢 我的搜索栏组件 class SearchBar extends Component { constructor(props){ super(props); this.handleSubmit = this.handleSubmit.bind(this); } handleSubmit(e){ e.pr
class SearchBar extends Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e){
e.preventDefault();
let reqBody = {
name: this.refs.name.value
};
fetch("/api", {
method: "POST",
body: JSON.stringify(reqBody),
headers: {"Content-Type": "application/json"}
})
.then((res) => {
if (res.ok){
return res.json();
} else {
throw new Error ('Something went wrong');
}
}).then((json) => {
console.log(json)
this.props.onDataFetched(json)
})
}
render() {
return (
<div >
<form onSubmit={this.handleSubmit}>
<input placeholder = "Enter Name" type="text" ref = "name"/>
<button type ="Submit"> Search</button>
</form>
</div>
);
}
}
类搜索栏扩展组件{
建造师(道具){
超级(道具);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(e){
e、 预防默认值();
让reqBody={
名称:this.refs.name.value
};
获取(“/api”{
方法:“张贴”,
正文:JSON.stringify(reqBody),
标题:{“内容类型”:“应用程序/json”}
})
。然后((res)=>{
如果(res.ok){
返回res.json();
}否则{
抛出新错误(“出了问题”);
}
})。然后((json)=>{
console.log(json)
this.props.onDataFetched(json)
})
}
render(){
返回(
搜寻
);
}
}
显示组件的组件
class History extends Component {
render() {
return (
<div >
<h2> History</h2>
<ul>
<li>
//display data here
</li>
</ul>
</div>
);
}
}
类历史扩展组件{
render(){
返回(
历史
-
//在此处显示数据
);
}
}
我的容器组件
class Container extends Component {
constructor(props){
super(props);
this.state ={
history : []
}
this.handleResultChange = this.handleResultChange.bind(this)
}
handleResultChange(history){
this.setState({
history,
})
}
render() {
return (
<div >
<SearchBar onDataFetched={this.handleResultChange}/>
<History data={this.state.history}/>
</div>
);
}
}
类容器扩展组件{
建造师(道具){
超级(道具);
这个州={
历史:[]
}
this.handleResultChange=this.handleResultChange.bind(this)
}
HandlerResultChange(历史记录){
这是我的国家({
历史
})
}
render(){
返回(
);
}
}
我真的建议您看看,它将使您的开发更容易,并且几乎是这种情况下的标准实现。我真的建议您看看,它将使您的开发更加容易,并且几乎是这种情况下的标准实现。您可以在搜索组件中处理表单提交。将搜索值(输入值)传递给容器组件
class SearchBar extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e){
e.preventDefault();
let reqBody = {
name: this.refs.name.value
};
console.log(reqBody);
this.props.onDataFetched(reqBody);
}
render() {
return (
<div >
<form onSubmit={this.handleSubmit}>
<input placeholder = "Enter Name" type="text" ref = "name"/>
<button type ="Submit"> Search</button>
</form>
</div>
);
}
}
class Container extends React.Component {
constructor(props){
super(props);
this.state ={
history : []
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(searchValue){
console.log(searchValue);
}
render() {
return (
<div >
<SearchBar onDataFetched={this.handleChange} />
</div>
);
}
}
ReactDOM.render(<Container />, mountNode )
类搜索栏扩展React.Component{
建造师(道具){
超级(道具);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(e){
e、 预防默认值();
让reqBody={
名称:this.refs.name.value
};
控制台日志(reqBody);
this.props.onDataFetched(reqBody);
}
render(){
返回(
搜寻
);
}
}
类容器扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
历史:[]
}
this.handleChange=this.handleChange.bind(this)
}
handleChange(搜索值){
console.log(searchValue);
}
render(){
返回(
);
}
}
ReactDOM.render(,mountNode)
现在说实现这一点的理想方法是如上所述,使用redux和redux表单
这将帮助您在将来节省大量时间,因为您将有一个正确的结构来完成这项工作。您可以在搜索组件中自行处理表单提交。将搜索值(输入值)传递给容器组件
class SearchBar extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e){
e.preventDefault();
let reqBody = {
name: this.refs.name.value
};
console.log(reqBody);
this.props.onDataFetched(reqBody);
}
render() {
return (
<div >
<form onSubmit={this.handleSubmit}>
<input placeholder = "Enter Name" type="text" ref = "name"/>
<button type ="Submit"> Search</button>
</form>
</div>
);
}
}
class Container extends React.Component {
constructor(props){
super(props);
this.state ={
history : []
}
this.handleChange = this.handleChange.bind(this)
}
handleChange(searchValue){
console.log(searchValue);
}
render() {
return (
<div >
<SearchBar onDataFetched={this.handleChange} />
</div>
);
}
}
ReactDOM.render(<Container />, mountNode )
类搜索栏扩展React.Component{
建造师(道具){
超级(道具);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit(e){
e、 预防默认值();
让reqBody={
名称:this.refs.name.value
};
控制台日志(reqBody);
this.props.onDataFetched(reqBody);
}
render(){
返回(
搜寻
);
}
}
类容器扩展了React.Component{
建造师(道具){
超级(道具);
这个州={
历史:[]
}
this.handleChange=this.handleChange.bind(this)
}
handleChange(搜索值){
console.log(searchValue);
}
render(){
返回(
);
}
}
ReactDOM.render(,mountNode)
现在说实现这一点的理想方法是如上所述,使用redux和redux表单
这将帮助您在将来节省大量时间,因为您将有一个正确的结构来执行此操作。最好在容器内调用API。您可以将搜索参数发送到容器并在那里进行Api调用。最好在容器内调用Api。您可以将搜索参数发送到容器并在那里执行Api调用。是否可以将我获取的JSON传递给显示数据的组件,或者我应该在容器中执行此操作?使用搜索值从容器中执行提取,与您在搜索组件中执行的提取调用相同,将这些值保持在状态,并将其作为属性传递给清单组件。我觉得将整个搜索结果json从搜索组件传递到容器组件会有点过分,所以在容器中进行提取也是如此。好的,谢谢你的工作。输入通过搜索传递,我能够从API获取数据。将我从容器中获取的数据发送到列表组件的最佳方式是什么?如上所述,将相同的数据作为属性传递给列表组件。当上述内容为alrite时,也可以查看Redux。是否可以将我获取的JSON传递给显示数据的组件,或者我应该在容器中执行此操作?是否可以使用搜索值从容器中进行的提取(与在搜索组件中执行的提取调用相同),将值保持在状态,并将其作为属性传递给列表组件。我觉得将整个搜索结果json从搜索组件传递到容器组件会有点过分,所以在容器中进行提取也是如此。好的,谢谢你的工作。输入通过搜索传递,我能够从API获取数据。什么