ทำไม ReactDOM.createPortal ไม่ได้ทำงานอยู่ในเนื้อหาภายในสคริปต์?

0

คำถาม

ฉันต้องเนื้อหาได้ที่นี่tsx แฟ้มด้วยการต่อรหัส:

import React from "react";
import {createPortal} from 'react-dom';

import Text from './Text';

console.log(`Content script...`);

createPortal(
    <Text/>,
    document.body
);

"ข้อความ"ส่วนประกอบรหัส:

import React from 'react';

const Text = () => {
    return (
        <div>
            Just text...
        </div>
    );
};

export default Text;

ของปรากฏออกรวม:

...other keys
"content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["./static/js/content.js"],
      "run_at": "document_end"
    }
]

อย่างที่คุณเห็นที่แฟ้มถูกโหลดแล้วข้อความ Content script ... คือพิมพ์ออกในคอนโซล. https://i.stack.imgur.com/GS0gK.png

แต่ div กับข้อความ Just text... ไม่ได้เพิ่มเข้ากับร่างกาย,หรือจะพูดอีกอย่าง createPortal ไม่ทำงาน https://i.stack.imgur.com/j2geh.png

1

คำตอบที่ดีที่สุด

1

คุณต้องการจะเขียน createPortal ข้างใน return หรือ render,

เหมือนนี้:

render() {
    return ReactDOM.createPortal(
         this.props.children,
         document.body
     );
}
2021-11-15 13:13:41

ฉันจะสร้างอะไรสักอย่างที่ codesandbox สำหรับคุณและมันทำงานไม่เป็นไรคุณสามารถตรวจสอบมันออก: codesandbox.บ io/s/wizardly-แคช-2vfby?แฟ้ม=/src/องเนื้อหาได้ที่นี่jsx
Pradip Dhakal

ฉักาเลือกตัวเลือกรหัสมันไม่ทำงานในคดีของฉันแต่ฉันสามารถเข้าใจธรรมชาติของปัญหาของฉันขอบคุณนะ
user17418364

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่