简体   繁体   English

反应:单击 select 选项时更改 state 值

[英]React: Change state value when select option items clicked

So In my react app, I have select menu and and two( width and height ) state variables.所以在我的反应应用程序中,我有 select 菜单和两个(宽度高度) state 变量。 Here is sandbox这里是沙盒

const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);

// select option

<div className="flex items-center space-x-2">
        <select
          id="countries"
          class="bg-gray-50 border w-96 border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block  p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
        >
         
          <option
            onClick={() => {
              setWidth(100);
              setHeight(200);
            }}
            selected
            value="header_ad"
          >
            Resolution 1
          </option>
          <option
            onClick={() => {
              setWidth(1000);
              setHeight(2000);
            }}
            selected
            value="header_ad"
          >
            Resolution 2
          </option>
        </select>
      </div>

I wanted to change the values of width and height when I clicked on the select option values.当我单击 select 选项值时,我想更改宽度高度的值。

ie When I click on Resolution 1 option I wanted to set the values of width and height to be 100 and 200 respectively and the same as if I clicked on Resolution 2 option.即当我单击Resolution 1选项时,我想将widthheight的值分别设置为100200 ,就像我单击Resolution 2选项一样。

How can I achieve this?我怎样才能做到这一点? I try to change their values with onClick event but it doesn't work.我尝试使用onClick事件更改它们的值,但它不起作用。

So option does not have onClick prop so you can do something like this:所以选项没有 onClick 道具所以你可以做这样的事情:

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [width, setWidth] = useState(0);
  const [height, setHeight] = useState(0);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div className="flex items-center space-x-2">
        <select
          id="countries"
          class="bg-gray-50 border w-96 border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block  p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
          onChange={(e) => {
            const selectedValue = e.target.value;
            if(selectedValue === "header_1"){
              setWidth(100);
              setHeight(200);
            }
            if(selectedValue === "header_2"){
              setWidth(1000);
              setHeight(2000);
            }
          }}
        >
          {/* {category?.categories?.map((catid, y) => (
              ))} */}
          <option value="default" disabled>
            Please select
          </option>
          <option value="header_1">HEADER AD111</option>
          <option value="header_2">222</option>
        </select>
      </div>
      <br></br>
      <span>Height:{height}</span>
      <br></br>
      <span>Width:{width}</span>
    </div>
  );
}

https://codesandbox.io/s/long-sunset-uow79l?file=/src/App.js:0-1434 https://codesandbox.io/s/long-sunset-uow79l?file=/src/App.js:0-1434

onClick on option element does NOT exist.选项元素上的 onClick 不存在。 Please check the following code snippet.请检查以下代码片段。

https://codesandbox.io/s/upbeat-wind-4i89o7?file=/src/App.jshttps://codesandbox.io/s/upbeat-wind-4i89o7?file=/src/App.js

    import { useState } from "react";
    import "./styles.css";

    const data = {
      option1: {
        width: 1024,
        height: 768
      },
      option2: {
        width: 100,
        height: 78
      }
    };

    export default function App() {
      const [width, setWidth] = useState(0);
      const [height, setHeight] = useState(0);

      const handleOnChange = (value) => {
        setWidth(data[value].width);
        setHeight(data[value].height);
      };

      return (
        <div className="App">
          <h1>Hello CodeSandbox</h1>
          <h2>Start editing to see some magic happen!</h2>
          <div className="flex items-center space-x-2">
            <select
              id="countries"
              className="bg-gray-50 border w-96 border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block  p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
              onChange={(e) => handleOnChange(e.currentTarget.value)}
            >
              <option selected value="option1">
                Option 1
              </option>
              <option value="option2">Option 2</option>
            </select>
          </div>
          <br></br>
          <span>Height:{height}</span>
          <br></br>
          <span>Width:{width}</span>
        </div>
      );
    }

you need to change your code to this code:您需要将代码更改为此代码:

<div className="flex items-center space-x-2">
        <select
          id="countries"
            onChange={() => {
              setWidth(1000);
              setHeight(2000);
            }}
          class="bg-gray-50 border w-96 border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block  p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
        >
         
          <option 
            selected
            value="header_ad"
          >
            Resolution 1
          </option>
          <option
            selected
            value="header_ad"
          >
            Resolution 2
          </option>
        </select>
      </div>

let me know if its work cheers !!让我知道它的工作是否欢呼!

change your event handler from onClick to onChange on selectbox.将您的事件处理程序从 onClick 更改为选择框上的 onChange。 in your event handler, check the selected option value and change you state accordingly.在您的事件处理程序中,检查选定的选项值并相应地更改您的 state。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM