Skip to content

optimize code editor component #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/components/CodeEditor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import React, { useState } from 'react';
import React, { useState, useCallback } from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Col, Row, Card, Button, Tooltip, OverlayTrigger } from '@themesberg/react-bootstrap';
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
Expand All @@ -15,14 +15,14 @@ export default (props) => {
const [copied, setCopied] = useState(false);
const noInline = code.includes('render(');

const handleCodeChange = (newCode) => {
const handleCodeChange = useCallback((newCode) => {
setCode(newCode);
};
}, [setCode]);

const handleCopy = () => {
const handleCopy = useCallback(() => {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
}, [setCopied]);

return (
<LiveProvider noInline={noInline} code={code} language={language} theme={themeStyle} scope={scope}>
Expand All @@ -34,11 +34,11 @@ export default (props) => {
</Card.Body>
</Card>
</Col>
{imports ? (
{imports && (
<Col xs={12} className="mb-4">
<Code code={imports} />
</Col>
) : null}
)}
<Col xs={12} className="mb-4">
<LiveError className="alert alert-danger" />

Expand All @@ -54,7 +54,7 @@ export default (props) => {

<LiveEditor onChange={handleCodeChange} className="live-editor" />

{copied ? <span className="text-success copy-code-text">Copied</span> : null}
{copied && <span className="text-success copy-code-text">Copied</span>}

<OverlayTrigger
trigger={['hover', 'focus']}
Expand Down