Javascript 无法通过react js中的道具发送状态的更新值
我正在尝试将状态值从父组件发送到子组件。最初,状态值为空。在API调用后,状态值将更新,但我的道具将使用初始空值Javascript 无法通过react js中的道具发送状态的更新值,javascript,reactjs,Javascript,Reactjs,我正在尝试将状态值从父组件发送到子组件。最初,状态值为空。在API调用后,状态值将更新,但我的道具将使用初始空值 class Metrics extends React.Component { constructor(props) { super(props); this.state.result:[]; } submit = e => { e.preventDefault(); var URL = "http://localhost:8080/f
class Metrics extends React.Component {
constructor(props) {
super(props);
this.state.result:[];
}
submit = e => {
e.preventDefault();
var URL =
"http://localhost:8080/fetchAnalysisData;
this.setState({ formsubmit: true });
fetch(URL, {
method: "GET"
})
.then(res => res.json())
.then(data => {
console.log("Response Success!!");
this.setState({ result: data });
});
};
render(){
return(
<Button onClick={this.submit}
fontSize: "small",
marginLeft: "30%"
}}
>
VIEW RESULT
</Button>
<div className={this.state.formsubmit ? "showDisplay" : "hide"}>
<DipslayBarChart result={this.state.result}></DipslayBarChart>
</div>
)
}
类度量扩展了React.Component{
建造师(道具){
超级(道具);
this.state.result:[];
}
提交=e=>{
e、 预防默认值();
变量URL=
"http://localhost:8080/fetchAnalysisData;
this.setState({formsubmit:true});
获取(URL{
方法:“获取”
})
.then(res=>res.json())
。然后(数据=>{
log(“响应成功!!”);
this.setState({result:data});
});
};
render(){
返回(
查看结果
)
}
子组件:
class DipslayBarChart extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div>
<BarChart width={500} height={300} data={this.props.result}>
<XAxis dataKey="Date" />
<YAxis />
<Tooltip cursor={false} />
<Legend />
<Bar dataKey="Success" stackId="a" fill="#068f12" label />
<Bar
dataKey="Failure"
stackId="a"
fill="#ec0b0b"
/>
</BarChart>
</div>
)
}
}
类DipslayBarChart扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
返回(
)
}
}
如何在状态更新后更新prop值?这样编写构造函数
constructor(props) {
super(props);
this.state={
result:[]
}
}
像这样编写构造函数
constructor(props) {
super(props);
this.state={
result:[]
}
}
此外,如果您没有在构造函数中使用道具,则无需声明构造函数
state={
result:[]
}
此外,如果您没有在构造函数中使用道具,则无需声明构造函数
state={
result:[]
}
当您使用console.log(数据)而不是console.log(“响应成功!”)时会得到什么?它更新了值当您使用console.log(数据)而不是console.log(“响应成功!”)时会得到什么?它更新了值