简体   繁体   中英

React spread operation of JSX elements inside map

I am mapping over a array inside a array for a Picker and am struggeling to figure out how to return the JSX elements rather than the JSX element array.

code example:

{modelA.map((mA) => {
    const pickerItems = mA.modelB.map((mA) =>
        <Picker.Item value={mA} ... />,
    );
    return pickerItems;
})}

usually my aproach would be to make use of spread operators. but they do not do well in this syntax.

this:

{...modelA.map((mA) => {
    const pickerItems = mA.modelB.map((mA) =>
        <Picker.Item value={mA} ... />,
    );
    return pickerItems;
})}

is illegal: Spread children are not supported in React.

My dirty take on this would be to configure it pre render. but i'd rather not.

any suggestions?

You can just use reduce:

{modelA.reduce((arr,mA) => {
    mA.modelB.forEach((mA) => {
        arr.push(<Picker.Item value={mA} ... />)
    });
    return arr;
}), []}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM