Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User can choose spell to cast #3

Open
ighellache opened this issue Feb 14, 2023 · 1 comment
Open

User can choose spell to cast #3

ighellache opened this issue Feb 14, 2023 · 1 comment

Comments

@ighellache
Copy link
Collaborator

No description provided.

@mariahrucker
Copy link

You can add a select element in the App.jsx file.

import React, { useState } from 'react';
import './App.css';

function App() {
  // Declare state variable for selected spell
  const [spell, setSpell] = useState('');

  // Event handler for when user selects a spell
  const handleSpellChange = (event) => {
    setSpell(event.target.value);
  };

  // Event handler for when user clicks "Cast Spell" button
  const handleCastSpell = () => {
    // Cast the selected spell 
    console.log(`Casting ${spell}`);
  };

  // Render the component
  return (
    <div className="App">
      <h1>Spell Caster</h1>
      <label htmlFor="spell-select">Choose a spell:</label>
      <select id="spell-select" value={spell} onChange={handleSpellChange}>
        <option value="">Select a spell</option>
        <option value="fireball">Fireball</option>
        <option value="frostbolt">Frostbolt</option>
        <option value="arcane-missiles">Arcane Missiles</option>
      </select>
      <button onClick={handleCastSpell}>Cast Spell</button>
    </div>
  );
}

export default App;

Created a select element with options for different spells. The value of the select element is controlled by the spell state variable using the useState hook. When the user selects a spell, the handleSpellChange function is called, which updates the spell state variable with the selected value.

Also created a button element with an onClick event that calls the handleCastSpell function. The function logs the selected spell to the console. You'll need to add CSS styles to the App.css file to style the select element and button element to match your app's design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

2 participants