【发布时间】:2020-02-16 14:14:38
【问题描述】:
我想从父组件访问嵌套组件。
这是 Bill Form.jsx
import BillDetailForm from './BillDetailForm';
render(){
return (
<form onSubmit={handleSubmit}>
<FieldArray
name= 'detail'
component={BillDetailForm}
placeholder= '...detail'
label='Detail'
/>
</form>
);
}
}
BillForm 是父组件。
这是 BillForm 的嵌套组件或子组件:BillDetailForm.jsx
render(){
return(
<form onSubmit={ handleSubmit }>
<div>Detail:</div>
<FieldArray
name= 'detail'
component={RenderDetail}
label='Detail'
/>
</form>
)
}
BillDetailForm 里面是 RenderDetail:
const RenderDetail = ({fields, meta: { error,submitFailed}},props) => (
<dl>
<dt>
<button type="button" className= 'btn btn-primary' onClick={() => fields.push()}>Add
Detail</button>
{submitFailed && error && <span>{error}</span>}
</dt>
{ fields.map((registerDetail, index) =>
//In the following line renderDetail is accesing Detail component.
<Detail detailItem={registerDetail} fields={fields} index={index} key={index}/>
)
}
{error && <dt className="error">{error}</dt>}
</dl>
);
这是细节类组件:
class Detail extends Component{
render(){
const{detailItem,index,fields,isSubtotal} = this.props;
return(
<dd key={index}>
<br></br>
<button className= 'btn btn-light mr-2'
type="button"
title="Remove detail"
onClick={() => { fields.remove(index)
if(fields.length == 0 || fields.length === undefined){
}
try{
for(let x in fields){
fields.remove(index)
let d = fields.selectedIndex;
if(fields.remove(index) && d >= 1 && d< fields.length ){
fields.removeAll(index);
}
}
}catch{console.info("deletes non numerical index")}
}}> Delete </button>
<h4>DetailRegister #{index + 1}</h4>
<Field
id={`${detailItem}._id`}
name={`${detailItem}.quantity`}
component= {NumberPickerInteger}
placeholder= '...quantity'
label = "Quantity"
/>
<br></br>
<h3><b>Product</b></h3>
<Field
id={`${detailItem}._id`}
name={`${detailItem}.product.code`}
type="number"
component= {RenderFieldNumeric}
placeholder='...Product's code'
label = "Product's code"
/>
<Field
id={`${detailItem}._id`}
name={`${detailItem}.product.name`}
type="text"
component= {RenderField}
placeholder='...Product's name'
label = "Product's name"
/>
<Field
id={`${detailItem}._id`}
name={`${detailItem}.product.price`}
component= {NumberPickerr}
placeholder= '...Price'
label = "Product's price"
/>
<br></br>
<h3><b>Subtotal</b></h3>
<Field
id={`${detailItem}._id`}
name={`${detailItem}.subtotal`}
component= {SubtotalWidget}
placeholder= '...subtotal'
label = "Subtotal"
>
{isSubtotal}
</Field>
</dd>
);
}
}
我想访问 BillForm 中的详细信息,例如 ${props.detailItem}.subtotal。 BillForm访问BillDetailForm,BillDetailForm访问renderDetail,最后一个renderDetail访问Detail。
问题是:如何通过 BillForm 的动态索引 (props.index) 访问和使用数量和小计等道具?我想从 BillForm 访问 Detail 组件,遵守以下顺序访问:BillForm -> BillDetailForm -> RenderDetail -> Detail
【问题讨论】:
-
为什么不为此目的使用状态管理?
-
不知道怎么用。在代码中会是怎样的?
-
当你处理 react 时,不要从 HTML 的角度考虑,要考虑要在周围共享的数据,现在状态将如何帮助解决这个问题,它将是所有数据所在的中心点,您可以从组件中的任何位置访问数据。请参阅 Redux。
-
如果你不想那么复杂,你也可以使用 React Hooks 之类的东西。 rangle.io/blog/simplifying-controlled-inputs-with-hooks
标签: javascript reactjs redux-form