Skip to content

Instantly share code, notes, and snippets.

@luismartinezs
Created August 26, 2023 09:33
Show Gist options
  • Save luismartinezs/0f61c131d12e87ce77cd68ff9d26e926 to your computer and use it in GitHub Desktop.
Save luismartinezs/0f61c131d12e87ce77cd68ff9d26e926 to your computer and use it in GitHub Desktop.
React Import on interaction #react #performance
// 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