【发布时间】:2018-07-07 16:24:11
【问题描述】:
我似乎无法弄清楚如何将我的道具从我的 App 组件记录到我的子 TotalBox 组件。它一直作为空对象返回。有谁知道如何成功地做到这一点?
App.js
class App extends React.Component {
constructor(props) {
super(props);
this.handler = this.handler.bind(this)
}
handler() {
this.setState({
value: 0
})
}
render() {
return (
<Wrapper>
<Grid>
<Row>
<CostBox/>
<Input/>
<TotalBox {...this.props}/>
</Row>
</Grid>
</Wrapper>
)
}
}
totalBox.js。
class TotalBox extends React.Component {
constructor(props) {
super(props);
console.log(this.props); // Object {}
this.state = {
value: this.props.value
}
}
render() {
return (
<Col md={3}>
<RightBox>
<TotalCostHeader>TOTAL COST</TotalCostHeader>
<TotalCostLabel>{this.state.value}</TotalCostLabel>
</RightBox>
</Col>
)
}
}
【问题讨论】:
-
你的 App 组件的 props 是什么?
-
App 组件中的
this.props是什么? -
我想传递
value的数值,以便在子组件和其他组件中对其进行操作 -
所以您在 App 组件中的某个位置有
this.props.value?编辑:在我看来你只设置了this.state.value -
我认为这就是我通过设置
value的状态所做的。
标签: javascript html reactjs constructor components