diff --git a/source/_posts/react.md b/source/_posts/react.md index c44a1c1..77c13e4 100644 --- a/source/_posts/react.md +++ b/source/_posts/react.md @@ -11,6 +11,7 @@ intro: | A React cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners. plugins: - copyCode + - runCode --- ## Getting Started @@ -62,7 +63,7 @@ return ( ```javascript import React from "react"; -export default function Weather(props) { +function Weather(props) { if (props.temperature >= 20) { return (

@@ -77,6 +78,8 @@ export default function Weather(props) { ); } } + +export default () => ; ``` Note: A component must always return something. @@ -140,14 +143,14 @@ Note: External components are found on npmjs.com and need to be imported first. ```javascript import React from "react"; -export default function Hello(props) { +function Hello(props) { function fullName() { return `${props.firstName} ${props.lastName}`; } return

{fullName()}

; } -; +export default () => ; ``` ## Properties {.cols-2} @@ -539,6 +542,7 @@ Note: Custom Hooks are reusable functions in React that contain logic shared acr to extract stateful logic from components into standalone functions. ### Creating Refs in Class Components + ```javascript import React, { Component } from "react"; @@ -561,8 +565,8 @@ export default MyComponent; ``` ### Using Refs in Functional Components -```javascript +```javascript import React, { useRef, useEffect } from "react"; function MyComponent() { @@ -579,15 +583,15 @@ export default MyComponent; ``` ### Callback Refs -```javascript +```javascript import React, { Component } from "react"; class MyComponent extends Component { constructor(props) { super(props); this.myRef = null; - this.setRef = element => { + this.setRef = (element) => { this.myRef = element; }; } @@ -605,6 +609,7 @@ export default MyComponent; ``` ### Forwarding Refs + ```javascript Copy code import React from "react"; @@ -621,10 +626,10 @@ const ref = React.createRef(); ``` ### Accessing DOM Elements with Refs + ### Refs are often used to access and interact with DOM elements directly. Here's an example where we focus an input element using a ref: ```javascript - import React, { useRef, useEffect } from "react"; function FocusInput() { @@ -642,10 +647,10 @@ export default FocusInput; ``` ### Managing Focus with Refs + ### You can also manage focus between multiple elements using refs: ```javascript - import React, { useRef } from "react"; function Form() { diff --git a/themes/coo/layout/_partial/scripts.ejs b/themes/coo/layout/_partial/scripts.ejs index e057c0c..57a39f3 100644 --- a/themes/coo/layout/_partial/scripts.ejs +++ b/themes/coo/layout/_partial/scripts.ejs @@ -80,6 +80,7 @@ // Get all pre > code elements const code_Blocks = document.querySelectorAll('pre > code'); const runInnerHTML = ''; + const pageHeader = (document.querySelector('h1')?.textContent || '').trim(); code_Blocks.forEach(function (codeBlock) { let content = codeBlock.textContent || ''; let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", ""); @@ -172,7 +173,7 @@ } } - if (document.querySelector('h1').textContent.includes('jQuery')) { + if (pageHeader.startsWith('jQuery')) { config = { ...baseConfig, markup: { @@ -185,7 +186,7 @@ } } - if (language === 'html' && document.querySelector('h1').textContent.includes('EJS')) { + if (language === 'html' && pageHeader.startsWith('EJS')) { config = { ...baseConfig, markup: { @@ -207,6 +208,21 @@ } } + if (language === 'javascript' && pageHeader.startsWith('React')) { + config = { + ...baseConfig, + script: { + language: 'jsx', + content, + }, + customSettings: { + imports: { + 'axios': 'https://cdn.skypack.dev/axios', + }, + }, + } + } + if (['php'].includes(language)) { config = { ...baseConfig,