3 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
我測(cè)試了這項(xiàng)工作。如果有孩子,我用 SwapableItems 包裹嵌套的孩子。
function SwapableItems({ children }) {
? const props = { style: { color: "red" } };
? return Children.map(children, (child) => {
? ? let nestedChild = child.props.children;
? ? const hasNestedChild = nestedChild && typeof nestedChild !== "string"
? ? if (hasNestedChild) {
? ? ? nestedChild = <SwapableItems>{nestedChild}</SwapableItems>;
? ? }
? ? return child.props?.swipeMe || hasNestedChild
? ? ? ? cloneElement(child, child.props?.swipeMe ? props : {}, [nestedChild])
? ? ? : child;
? });
}

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
child.props.children是一個(gè)數(shù)組,所以React.isValidElement(child.props.children)總是虛假的,要修復(fù)它,請(qǐng)嘗試React.cloneElement在其上使用:
React.isValidElement(React.cloneElement(child.props.children)); // true
樣式重置示例:
const styleA = {
color: "blue"
};
const InlineReset = ({ children }) => {
return React.Children.map(children, child => {
console.log(child.props);
return React.cloneElement(child.props.children, { style: null });
});
};
export default function App() {
return (
<InlineReset>
<div>
<h1 style={styleA}>Hello </h1>
</div>
</InlineReset>
);
}

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
這是解決方案:
我已經(jīng)初始化了一個(gè)variable,它將保存recursion邏輯的外部并且未定義。
let childHasChildren;
我已經(jīng)將代碼封裝在一個(gè)if聲明中并進(jìn)行了一些修改:“如果child有孩子,要么是 要么array,object如果在傳遞的范圍內(nèi)children有/是有效React元素”
const deeperChildren = child.props.children;
? ? const arrayOfChildren = Array.isArray(deeperChildren);
? ? const singleChildren =
? ? ? typeof deeperChildren === "object" && deeperChildren !== null;
? ? if (
? ? ? (arrayOfChildren &&
? ? ? ? deeperChildren.some((c) => React.isValidElement(c))) ||
? ? ? (singleChildren && React.isValidElement(deeperChildren))
? ? ) {
為了不出錯(cuò)地傳遞遞歸,以防語句中的代碼if被調(diào)用,我克隆了一個(gè)過濾器/單個(gè)對(duì)象,其子對(duì)象將是 React 有效元素,然后我只將這些/這個(gè)傳遞到遞歸中:
const validChildren = arrayOfChildren
? ? ? ? ? deeperChildren.filter((c) => React.isValidElement(c))
? ? ? ? : deeperChildren;
現(xiàn)在它接受一個(gè)有孩子的項(xiàng)目,或者一個(gè)數(shù)組。這可以配置為動(dòng)態(tài)傳遞道具,默認(rèn)道具和其他可以從組件外部傳遞的道具,盡管后者不是我的情況。為了在不使用這些解決方案的情況下實(shí)現(xiàn)更復(fù)雜的東西,例如 render?props
、props contracts、HOCs 等,需要這個(gè)解決方案。
const childrenHandler = (children, swipeMeProps) => {
? const childEls = React.Children.toArray(children).map((child) => {
? ? let childHasChildren;
? ? const deeperChildren = child.props.children;
? ? const arrayOfChildren = Array.isArray(deeperChildren);
? ? const singleChildren =
? ? ? typeof deeperChildren === "object" && deeperChildren !== null;
? ? if (
? ? ? (arrayOfChildren &&
? ? ? ? deeperChildren.some((c) => React.isValidElement(c))) ||
? ? ? (singleChildren && React.isValidElement(deeperChildren))
? ? ) {
? ? ? const validChildren = arrayOfChildren
? ? ? ? ? deeperChildren.filter((c) => React.isValidElement(c))
? ? ? ? : deeperChildren;
? ? ? childHasChildren = childrenHandler(validChildren, swipeMeProps);
? ? }
? ? return childHasChildren
? ? ? ? React.cloneElement(child, {}, childHasChildren)
? ? ? : child.props.swipeMe
? ? ? ? React.cloneElement(child, { ...swipeMeProps })
? ? ? : React.cloneElement(child, {});
? });
? return childEls;
};
添加回答
舉報(bào)