Created
August 26, 2023 09:33
-
-
Save luismartinezs/0f61c131d12e87ce77cd68ff9d26e926 to your computer and use it in GitHub Desktop.
React Import on interaction #react #performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source https://www.patterns.dev/posts/import-on-interaction | |
import React, { useState, createElement } from "react"; | |
import MessageList from "./MessageList"; | |
import MessageInput from "./MessageInput"; | |
import ErrorBoundary from "./ErrorBoundary"; | |
const Channel = () => { | |
const [emojiPickerEl, setEmojiPickerEl] = useState(null); | |
const openEmojiPicker = () => { | |
import(/* webpackChunkName: "emoji-picker" */ "./EmojiPicker") | |
.then((module) => module.default) | |
.then((emojiPicker) => { | |
setEmojiPickerEl(createElement(emojiPicker)); | |
}); | |
}; | |
const closeEmojiPickerHandler = () => { | |
setEmojiPickerEl(null); | |
}; | |
return ( | |
<ErrorBoundary> | |
<div> | |
<MessageList /> | |
<MessageInput onClick={openEmojiPicker} /> | |
{emojiPickerEl} | |
</div> | |
</ErrorBoundary> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment