Benutzer-Werkzeuge

Webseiten-Werkzeuge


ibex:kursinhalte:christian:start

Dies ist eine alte Version des Dokuments!


const [bugs,setBugs] = setState(
  {id:1, titel: 'Bug1', fixed: false}
  {id:2, titel: 'Bug2', fixed: false}
);
 
setBugs([...bugs].map((bug) => (bug.fixed === false ? {...bug, fixed: true}: bug )));
 
//setBugs(bugs.map((bug) => (bug.fixed === false ? {...bug, fixed: true}: bug )));
 
// setBugs(bugs.map(bug) => (bug = (bugs.id === 2 ? {...bug,fixed = true} : bug ) ) )
setBugs(bugs.map(bug) => (bug = {...bug,fixed: true})
 
 
setUser({...user,adress: {...user.adress, zipCode: 94112}});

nachschauen.txt

// Updating Arrays
 
 const [mood, setMood] = useState(['happy', 'cheerful']);
 
    console.log(mood);
 
    const handleCLick = () => {
        // Neues Array item hinzufügen
        mood.push('sad'); // Tu das nicht! Es verändert das original Array
 
        setMood([...mood, 'sad']);
 
        // Item aus Array entfernen
        setMood([...mood].filter((mood) => mood !== 'happy'));
 
        // Update Item in Array
        setMood(mood.map((mood) => (mood === 'sad' ? 'blub' : mood)));
    };
 
 
// Updating Objects
 
 const [cocktail, setCocktail] = useState({
        title: 'Old Fashioned',
        price: 7,
    });
 
    console.log(cocktail);
 
    const handleClick = () => {
        cocktail.price = 8; // nicht tun!
        setCocktail(cocktail); // funktioniert nicht
 
        const newPrice = {
            title: cocktail.title, // spread cocktail object
            price: 9,
        };
 
        setCocktail(newPrice);
        // oder
        setCocktail({ ...cocktail, price: 9 });
    };
 
 
 
// Updating nested Objects
 
const [user, setUser] = useState({
        name: 'John',
        lastName: 'Doe',
        address: {
            city: 'San Francisco',
            zipCode: 94111,
        },
    });
 
    console.log(user);
 
    const handleCLick = () => {
        setUser({ ...user, address: { ...user.address, zipCode: 94112 } });
    };
 
 
 
// Updating Object Arrays
 
const [bugs, setBugs] = useState([
        { id: 1, title: 'Bug 1', fixed: false },
        { id: 2, title: 'Bug 2', fixed: false },
    ]);
 
    console.log(bugs);
 
    const handleCLick = () => {
        // Update Props in Array
        setBugs(bugs.map((bug) => (bug.id === 2 ? { ...bug, fixed: true } : bug)));
    };
/volume1/web/dokuwiki/data/attic/ibex/kursinhalte/christian/start.1740856990.txt.gz · Zuletzt geändert: 2025/03/01 20:23 von 20.171.207.133