第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將道具傳遞給 React 中的特定嵌套子項(xiàng)

將道具傳遞給 React 中的特定嵌套子項(xiàng)

牧羊人nacy 2023-06-09 15:06:54
我正在嘗試為孩子們配置一些道具。在這個(gè)虛擬示例中,我正在測(cè)試該函數(shù),以便使用以下屬性專門針對(duì)任何嵌套或未嵌套的子對(duì)象:swipeMe。div如果在 my on the里面render function它只包含一個(gè)孩子,它工作得很好,就像這樣:? ? <SwapableItems>? ? ? <div>? ? ? ? {/*the p element will get the red color as defined on childrenHandler*/}? ? ? ? <p swipeMe>Add Props</p>?? ? ? </div>? ? </SwapableItems>然而,如果我在我的中添加更多的孩子div,不知何故我猜我的三元操作childrenHandler效果不佳,我不知道為什么......如果它有孩子,克隆這個(gè)元素并調(diào)用childrenHandler傳遞它的孩子。如果它有我想要的道具,克隆元素并用它做點(diǎn)什么。如果以上都不是,則只需克隆該元素。return?childHasChildren ????????React.cloneElement(child,?{},?childHasChildren) ??????:?child.props.swipeMe ????????React.cloneElement(child,?{?...swipeMeProps?}) ??????:?React.cloneElement(child,?{});下面是完整的腳本。import React from "react";import ReactDOM from "react-dom";function App() {? return (? ? <SwapableItems>? ? ? <div>? ? ? ? <p swipeMe>Add Props</p>? ? ? ? <div>Don't Add Props</div>? ? ? </div>? ? </SwapableItems>? );}function SwapableItems({ children }) {? const content = childrenHandler(children, { style: { color: "red" } });? return content;}const childrenHandler = (children, swipeMeProps) => {? const childEls = React.Children.toArray(children).map((child) => {? ? const childHasChildren =? ? ? child.props.children && React.isValidElement(child.props.children)? ? ? ? ? childrenHandler(child.props.children, swipeMeProps)? ? ? ? : undefined;? ? return childHasChildren? ? ? ? React.cloneElement(child, {}, childHasChildren)? ? ? : child.props.swipeMe? ? ? ? React.cloneElement(child, { ...swipeMeProps })? ? ? : React.cloneElement(child, {});? });? return childEls;};const rootElement = document.getElementById("root");ReactDOM.render(<App />, rootElement);
查看完整描述

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;

? });

}


查看完整回答
反對(duì) 回復(fù) 2023-06-09
?
呼喚遠(yuǎn)方

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>

  );

}


查看完整回答
反對(duì) 回復(fù) 2023-06-09
?
九州編程

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;

};


查看完整回答
反對(duì) 回復(fù) 2023-06-09
  • 3 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)