状態(私のケースでは提出)が変更されたときにコンポーネントを変更しようとしていました。これまで私は状態を正常に更新していますが(HTTP getではなく)正しくコンポーネントが変更されますが、httpで呼び出した状態が更新されてもコンポーネントは変更されません。この問題を解決するための提案をお願いします。事前のおかげで.... :)私のコンポーネントが反応で再レンダリングされない理由
import React from 'react';
import { Panel, Grid, Row, Col } from 'react-bootstrap';
import styles from './Panels.scss';
import Card from '../Card/Card.js';
import PanelForm from '../PanelForm/PanelForm';
import PanelFormData from '../PanelFormData/PanelFormData';
import http from 'axios';
export default class Panels extends React.Component {
constructor(props) {
super(props);
this.state={
submitted:0,
allocatedBudgetData:this.props.allocatedBudgetData
}
this.updateStateForm=this.updateStateForm.bind(this);
}
//this function is called by another component and update the state
updateStateForm(){
http.get('./static/PanelReportDataFilled.json')
.then((response) => {
var submitted=this.state.submitted==0?1:0;
console.log('inside http method',submitted);
this.setState({
allocatedBudgetData:response.data,
submitted:submitted
});
})
}
render() {
console.log('inside render state is ',this.state.submitted);
const panelFormInput=(<PanelForm formState={this.state.submitted} formType={this.updateStateForm} role={this.props.role} allocatedBudgetData={this.state.allocatedBudgetData}></PanelForm>);
return(
<Panel header={this.props.title} bsStyle="primary" className={styles['panels']}>
<Grid>
<Row className="show-grid">
<Col sm={5} md={8}>
<div style={{height: "auto"}}>
<Card renderchild={panelFormInput} borderRadius={4}/>
</div>
</Col>
</Row>
</Grid>
</Panel>
)
}
}
私は@harkiratが示唆するように、あなたが示唆してaxiosへのhttpの名前を変更します。また、エラーがスローされず、反応によって飲み込まれていないことを確認するために、アクシオスコールにキャッチを追加することをお勧めします。最後に、 "Card" jsxを投稿して、updateStateFormの呼び出しを妨げているタイプミスがないかどうかを調べることができます。 –
@Paulstoner問題は、updateStateform()の内部で状態を変更しているときで、httpが正常に動作しているとコメントしたときです。しかし、私はそれの中でそれを行うとき、それは状態を変えましたが、コンポーネントを変更しませんでした。だから、カードに問題があります。 – redblood
私の謝罪。私はもはや助けになることに反応するには新しすぎです。私がこのように学んだのは、コンポーネントが状態の変化に応じて更新されるということです。私は、コンポーネントが更新されなければ、状態の変化がどのように子供に伝えられているかにいくつかの問題があるという難しい方法を学びました。 –