This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createResource, For, Match, Switch, Show } from "solid-js"; | |
import { render } from "solid-js/web"; | |
// Define an asynchronous function to fetch user data from an API endpoint | |
const fetchUser = async () => { | |
// Fetch user data from the API | |
const response = await fetch(`https://dummyjson.com/users`); | |
// Parse the JSON response | |
const jsonResponse = await response.json(); | |
// Return the array of users from the JSON response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createSignal(initialValue) { | |
let value = initialValue; // Internal state to hold the current value of the signal | |
const subscribers = new Set(); // Set to keep track of subscribers (components or functions that depend on the signal) | |
// Getter function to retrieve the current value of the signal | |
function get() { | |
return value; | |
} | |
// Setter function to update the value of the signal and notify subscribers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Signal { | |
constructor(initialValue, dependencyGraph) { | |
this.value = initialValue; | |
this.dependencyGraph = dependencyGraph; | |
} | |
// Method to set a new value and notify subscribers | |
setValue(newValue) { | |
this.value = newValue; | |
// Notify subscribers through the dependency graph |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array to keep track of currently executing computations | |
const context = []; | |
// Function to add a computation (running) to the subscriptions of a signal | |
function subscribe(running, subscriptions) { | |
// Add the computation to the subscriptions set | |
subscriptions.add(running); | |
// Add the subscriptions set to the dependencies of the computation | |
running.dependencies.add(subscriptions); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
// Function to create a reactive store | |
function createStore(initialState) { | |
// Store the initial state in a private variable | |
let state = initialState; | |
// Array to hold subscribers (callbacks) | |
const subscribers = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStore(initialState) { | |
// Store the initial state in a private variable | |
let state = initialState; | |
// Array to hold subscribers (callbacks) | |
const subscribers = []; | |
// Create a proxy for the state object | |
const stateProxy = new Proxy(state, { | |
// Trap for getting a property value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to create a reactive store | |
function createStore(initialState) { | |
// Store the initial state in a private variable | |
let state = initialState; | |
// Create a proxy for the state object | |
const stateProxy = new Proxy(state, { | |
// Trap for getting a property value | |
get: function(target, property, receiver) { | |
console.log(`Getting property '${property}'`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { onMount } from 'svelte'; | |
// Define an array of tasks | |
let tasks = [ | |
{ id: 1, title: 'Complete homework', completed: false }, | |
{ id: 2, title: 'Do laundry', completed: false }, | |
{ id: 3, title: 'Buy groceries', completed: false } | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Parent.svelte --> | |
<!-- Script Section --> | |
<script> | |
// Import necessary modules | |
import axios from 'axios'; // Import axios for making HTTP requests | |
import Child from './Child.svelte'; // Import the Child component | |
// Function to handle form submission | |
const handleSubmit = async (event) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Child.svelte --> | |
<!-- Script Section --> | |
<script> | |
// Import necessary functions from Svelte | |
import { createEventDispatcher } from 'svelte'; | |
// Create a dispatcher for emitting custom events | |
const dispatch = createEventDispatcher(); |