Public
Edited
Apr 3
1 fork
Insert cell
Insert cell
import {
render,
jsx,
component,
useState,
useEffect,
useRef
} from "@j-f1/react-16"
Insert cell
//This works
function SimpleHelloWorld() {
return jsx`
<div>
Hello World!
</div>
`;
}
Insert cell
render((_) => jsx`<${SimpleHelloWorld}/>`)
Insert cell
//This fails
function SimpleHelloWorld2() {
var ref = useState();
return jsx`
<div>
Hello World!
</div>
`;
}
Insert cell
function CounterComponent() {
// 1. Initialize state using useState with an initial value of 0
// 2. Destructure the returned array into the state variable (count)
// and the setter function (setCount)
const [count, setCount] = useState(0);

// 3. Define a function to handle the button click
const increment = () => {
// 4. Use the setter function to update the state.
// Using a function ensures we update based on the *previous* state.
setCount((prevCount) => prevCount + 1);
};

// 5. Return JSX using the jsx tagged template literal
return jsx`
<div>
<h2>Counter Example</h2>
<p>Current Count: ${count}</p>
<button onClick=${increment}>
Increment +
</button>
</div>
`;
}
Insert cell
render((_) => jsx`<${CounterComponent}/>`)
Insert cell
render((_) => jsx`<${HelloWorldComponent}/>`)
Insert cell

function HelloWorldComponent() {
// Create a ref object using the useRef hook properly
const messageRef = useRef({
message: "Hello, World!",
greetings: {
english: "Hello, World!",
spanish: "¡Hola, Mundo!",
french: "Bonjour, Monde!"
},
language: "english",
addLanguage: function(language) {
this.greetings[language] = this.greetings[language] || "";
},
removeLanguage: function(language) {
if (language in this.greetings && Object.keys(this.greetings).length > 1) {
delete this.greetings[language];
}
},
update: function() {
console.log("Updated message configuration");
}
});
const [currentGreeting, setCurrentGreeting] = useState(messageRef.current.message);
const [currentLanguage, setCurrentLanguage] = useState("english"); //default language

const handleGreetingChange = (newGreeting) => {
messageRef.current.message = newGreeting;
setCurrentGreeting(newGreeting);
messageRef.current.update();
};

const addNewLanguage = (newLanguage) => {
messageRef.current.addLanguage(newLanguage);
messageRef.current.update();
};

const deleteLanguage = (languageName) => {
if (currentLanguage === languageName) {
setCurrentLanguage(Object.keys(messageRef.current.greetings)[0]);
messageRef.current.language = Object.keys(messageRef.current.greetings)[0];
}
messageRef.current.removeLanguage(languageName);
messageRef.current.update();
};
const handleLanguageAdd = (languageName) => {
messageRef.current.addLanguage(languageName);
messageRef.current.update();
};

const handleLanguageRemove = (languageName) => {
messageRef.current.removeLanguage(languageName);
messageRef.current.update();
};

return jsx`
<div>
<${Header}
onLanguageAdd={handleLanguageAdd}
onLanguageRemove={handleLanguageRemove}
onGreetingChange=${handleGreetingChange}
messageRef=${messageRef}
addNewLanguage=${addNewLanguage}
deleteLanguage=${deleteLanguage}
currentLanguage=${currentLanguage}
setCurrentLanguage=${setCurrentLanguage}
currentLanguage={currentLanguage}
/>
<${Display} messageRef=${messageRef} currentGreeting=${currentGreeting} currentLanguage=${currentLanguage}/>
</div>
`;
}
Insert cell
Insert cell
function Display({ messageRef, currentGreeting, currentLanguage }) {
const getBackgroundColor = () => {
const colorMap = {
english: "#e0f7fa",
spanish: "#fff9c4",
french: "#e8eaf6",
german: "#f1f8e9",
japanese: "#fce4ec"
};

return colorMap[currentLanguage] || "#f5f5f5";
};

return jsx`
<div className="display" style=${{
backgroundColor: getBackgroundColor(),
padding: "2rem",
margin: "1rem 0",
borderRadius: "8px",
textAlign: "center",
fontSize: "2rem",
fontWeight: "bold",
transition: "background-color 0.3s ease"
}}>
<h1>${currentGreeting}</h1>
<p>${
currentLanguage.charAt(0).toUpperCase() + currentLanguage.slice(1)
} greeting</p>
</div>
`;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more