feat: add support for running react

This commit is contained in:
Hatem Hosny committed 2025-01-05 17:12:31 +02:00
1 parent d878467327
commit 870fe5177b
2 files changed
+31 -10

No files matched your search

+13 -8
View File
@@ -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 (
<p>
@@ -77,6 +78,8 @@ export default function Weather(props) {
);
}
}
export default () => <Weather city="New York" temperature={24} />;
```
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 <p>{fullName()}</p>;
}
<Hello firstName="Matt" lastName="Delac" />;
export default () => <Hello firstName="Matt" lastName="Delac" />;
```
## 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() {
+18 -2
View File
@@ -80,6 +80,7 @@
// Get all pre > code elements
const code_Blocks = document.querySelectorAll('pre > code');
const runInnerHTML = '<svg height="1em" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g stroke-width="0"></g><g stroke-linecap="round" stroke-linejoin="round"></g><g><path d="M16.6582 9.28638C18.098 10.1862 18.8178 10.6361 19.0647 11.2122C19.2803 11.7152 19.2803 12.2847 19.0647 12.7878C18.8178 13.3638 18.098 13.8137 16.6582 14.7136L9.896 18.94C8.29805 19.9387 7.49907 20.4381 6.83973 20.385C6.26501 20.3388 5.73818 20.0469 5.3944 19.584C5 19.053 5 18.1108 5 16.2264V7.77357C5 5.88919 5 4.94701 5.3944 4.41598C5.73818 3.9531 6.26501 3.66111 6.83973 3.6149C7.49907 3.5619 8.29805 4.06126 9.896 5.05998L16.6582 9.28638Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"></path></g></svg>';
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,