如何将变量的值从子组件发送到父组件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将变量的值从子组件发送到父组件?相关的知识,希望对你有一定的参考价值。
我需要从CSV文件加载数据(参见下面提供的CSVDataTable.js
)并将这些数据传递给变量BatchFlights.js
中的主要组件this.state.csvData
。然后我在this.state.csvData
的API请求中使用fetchData()
。
组件CSVDataTable.js
正确加载数据。但是,如何从BatchFlights.js
的handleChange
访问它?
BatchFlights.js
import React, { Component, Fragment } from 'react';
import TopControls from "./layout/batch/TopControls"
import MainContent from "./layout/batch/MainContent"
import styles from "./layout/styles/styles";
import { withStyles } from "@material-ui/core/styles";
class BatchFlights extends Component {
constructor(props) {
super(props);
this.state = {
holdingTime: 1,
csvData: [],
delay: 0,
delay_probability: 0,
delay_cat: "NA",
labelWidth: 0
};
this.handleChange = this.handleChange.bind(this);
};
componentDidMount() {
this.fetchData();
};
fetchData = () => {
const url = "http://localhost:8000?"+
'holdingTime='+this.state.holdingTime+
'&csvData='+this.state.csvData;
fetch(url, {
method: "GET",
dataType: "JSON",
headers: {
"Content-Type": "application/json; charset=utf-8",
}
})
.then((resp) => {
return resp.json()
})
.then((data) => {
this.updateDelay(data.prediction,data.probability)
})
.catch((error) => {
console.log(error, "catch the hoop")
})
};
handleChange = (name, event) => {
this.setState({
[name]: event.target.value
}, () => {
console.log("csvData", this.state.csvData)
});
};
render() {
return (
<Fragment>
<TopControls state={this.state} styles={this.props.classes} handleChange={this.handleChange} />
<MainContent state={this.state} styles={this.props.classes} />
</Fragment>
);
}
}
const StyledBatchFlights = withStyles(styles)(BatchFlights);
export default StyledBatchFlights;
布局/批次/ MainContent.js
import React, { Component, Fragment } from 'react';
import CssBaseline from '@material-ui/core/CssBaseline';
import CSVDataTable from './CSVDataTable';
class MainContent extends Component {
render() {
return (
<Fragment>
<CssBaseline />
<main className={this.props.styles.mainPart}>
<CSVDataTable />
</main>
</Fragment>
);
}
}
布局/批次/ CSVDataTable.js
import React, { Component } from 'react';
import { CsvTohtmlTable } from 'react-csv-to-table';
import ReactFileReader from 'react-file-reader';
import Button from '@material-ui/core/Button';
const sampleData = `
NUM,WAKE,PLANNED_TURNAROUND,SIBT,SOBT,DISTANCE_FROM_ORIGIN,DISTANCE_TO_TARGET
1,M,45,2016-01-01 04:05:00,2016-01-01 14:10:00,2000,2000
`;
class CSVDataTable extends Component {
state={
csvData: null
};
handleFiles = files => {
var reader = new FileReader();
reader.onload = (e) => {
// Use reader.result
this.setState({
csvData: reader.result
})
}
reader.readAsText(files[0]);
}
render() {
return <div>
<ReactFileReader
multipleFiles={false}
fileTypes={[".csv"]}
handleFiles={this.handleFiles}>
<Button
variant="contained"
color="primary"
>
Load data
</Button>
</ReactFileReader>
<CsvToHtmlTable
data={this.state.csvData || sampleData}
csvDelimiter=","
tableClassName="table table-striped table-hover"
/>
</div>
}
}
export default CSVDataTable;
更新:
setCsvData = csvData => {
this.setState({
csvData
}, () => {
console.log("csvData",this.state.csvData)
});
}
以上是关于如何将变量的值从子组件发送到父组件?的主要内容,如果未能解决你的问题,请参考以下文章
如何从子组件 n Angular 将 FormGroup 对象作为输出发送到父组件