简体   繁体   English

如何使用 onclick 函数更新我的状态?

[英]how do i update my state using a function onclick?

I'm making a simple rock paper scissors app I have an array of 'rock', 'paper', and 'scissors, and I'm mapping it.我正在制作一个简单的石头剪刀布应用程序,我有一组“石头”、“纸”和“剪刀”,我正在绘制它。 but the problem is that when i click the button it should update the state through the handle click function and should display user choice但问题是,当我单击按钮时,它应该通过句柄单击功能更新状态并显示用户选择

here is my app component:这是我的应用程序组件:

import { useState } from "react";

const App = () => {
  const [userChoice, setuserChoice] = useState(null);
  const choices = ["rock", "paper", "scissors"];
  const handleClick = (value) => {
    console.log('hi');
    setuserChoice(value);
  };
  return (
    <div>
      <h1>Userchoice is:</h1>
      <h1>compute choice is :</h1>
      <>
      {choices.map(choice  => <button key={choice} onClick={()=>handleClick(choice)}>{choice}</button>)}
      
      </>


    </div>
  );
};

export default App;

here is my index.js:这是我的 index.js:

 import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

I was having trouble rendering the list using the map what worked for that was wrapping my map function in a fragment im new to reacting so I'm not sure.我在使用地图渲染列表时遇到了麻烦,为此我将地图函数包装在一个片段中,我不知道如何做出反应,所以我不确定。 what i want is when someone clicks on the rock button it should update the state and display rock as the user choice.我想要的是当有人点击摇滚按钮时,它应该更新状态并显示摇滚作为用户选择。

Everything is just right, you just forgot to render userChoice state.一切都刚刚好,你只是忘了渲染 userChoice 状态。

import { useState } from 'react';

const App = () => {
const [userChoice, setuserChoice] = useState(null);
const choices = ['rock', 'paper', 'scissors'];
const handleClick = (value) => {
    console.log('hi');
    setuserChoice(value);
};
return (
    <div>
        <h1>Userchoice is: {userChoice}</h1>
        <h1>compute choice is :</h1>
        <>
            {choices.map((choice) => (
                <button key={choice} onClick={() => handleClick(choice)}>
                    {choice}
                </button>
            ))}
        </>
    </div>
);
};

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 onClick 函数在子组件中设置父组件的状态? - How do I set the state of a parent component in a child component using an onClick function? 如何在 React 中更新我的 state 数组而不更新 function - How do I update my state array without having update function in React React:如何从外部函数更新组件状态? - React: How do I update my components state from an external function? 我可以使用操作和减速器来更新状态,但是当我尝试对其引发的操作执行onClick时,可以在组件上更新状态 - I am able to update the state using action and reducer but on the component when I try to do onClick for the action it throws 如何更改 state onClick? - How do I change state onClick? 我如何更新状态以使用 array.map 函数更新数组内对象的值 - how do i update state in react for updating the value of an object inside an array using array.map function 渲染前如何更新状态? - How do i update my state before it renders? 如何立即更新React状态? - How do I make my React state update immediately? 如何防止 state 对反应 onClick function (外部 useEffect)的更新? - How to prevent a state update on a react onClick function (outside useEffect)? 如何通过 onClick function 在该上下文 onClick function 中创建在该上下文 onClick - How do I pass a React Context's state through an onClick function created in a method inside that context class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM