简体   繁体   English

字符串数组未在反应中呈现

[英]string array not rendering in react

I want to see my array rendered everytime I select an option from my dropdown.每次从下拉列表中选择一个选项时,我都希望看到我的数组呈现。 But, it doesn't show immediately on changing elements in dropdown.但是,它不会在下拉列表中更改元素时立即显示。 How can I solve this?我该如何解决这个问题?

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

const removeTags = (tagToRemove, currentTag, setCurrentTag) => {
  let temp = currentTag;
  setCurrentTag(temp.filter(each => each !== currentTag));
};

export default function App() {
  const [currentTag, setCurrentTag] = useState([]);

  return (
    <div>
      <select
        onChange={e => {
          let temp = currentTag;
          temp.push(e.target.value);
          console.log(temp);
          setCurrentTag(temp);
        }}
      >
        <option>1</option>
        <option>2</option>
        <option>3</option>
      </select>
      {currentTag !== null
        ? currentTag.map((each, index) => (
            <span
              key={index}
              className="tag"
              style={{ backgroundColor: "white" }}
            >
              <span className="tag is-link" style={{ marginLeft: ".5em" }}>
                {each}
              </span>
              <a
                className="tag is-delete"
                onClick={() => removeTags(each, currentTag, setCurrentTag)}
              />
            </span>
          ))
        : ""}
    </div>
  );
}

Here is my code, codesandbox这是我的代码, codeandbox

The function to update state value setCurrentTag accepts a new state value .更新状态值 setCurrentTag 的函数接受一个新的状态值

  <select
    onChange={e => {
      setCurrentTag([...currentTag, e.target.value]);
    }}
  >
    <option>1</option>
    <option>2</option>
    <option>3</option>
  </select>

Your mistake was to mutate state value and pass it to setCurrentTag instead of creating a new value您的错误是改变状态值并将其传递给 setCurrentTag 而不是创建新值

  onChange={e => {
    let temp = currentTag; // currentTag is an array, so temp store reference to that array, and not a new array

    temp.push(e.target.value); // push method changes array temp, which refers to currentTag

    console.log(temp);
    setCurrentTag(temp); // pass the same mutated currentTag array
 }}

https://codesandbox.io/s/st-of-react-array-example-tdts8 https://codesandbox.io/s/st-of-react-array-example-tdts8

To add items to array state you can use setState like that:要将项目添加到数组状态,您可以像这样使用setState

setCurrentTag(old => [...old, newItem]);

And to remove item from the array use like this:并从数组中删除项目使用如下:

setCurrentTag(old => old.filter(a => a != itemToRemove));

Here's sandbox .这是沙箱

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

export default function App() {
  const [currentTag, setCurrentTag] = useState([]);

  return (
    <div>
      <select
        onChange={e => {
          let value = e.target.value;
          setCurrentTag(old => [...old, value]);
        }}
      >
        <option>1</option>
        <option>2</option>
        <option>3</option>
      </select>
      {currentTag !== null
        ? currentTag.map((each, index) => (
            <span
              key={index}
              className="tag"
              style={{ backgroundColor: "white" }}
            >
              <span className="tag is-link" style={{ marginLeft: ".5em" }}>
                {each}
              </span>
              <a
                className="tag is-delete"
                onClick={() => {
                  setCurrentTag(old => old.filter(a => each !== a));
                }}
              />
            </span>
          ))
        : ""}
    </div>
  );
}
import React, { useState } from "react";
import { isEmpty } from "lodash";

function addTags(e, value, setCurrentTag, setAllSectorsSelected) {
  console.log("in addTags,e,value: ", e, value);
}

const removeTags = (tagToRemove, currentTag, setCurrentTag) => {
  let temp = currentTag;
  setCurrentTag(temp.filter(each => each !== currentTag));
};

export default function App() {
  const [currentTag, setCurrentTag] = useState([]);

  const tagSelector = event => {
    // spreading over the previously added options
    setCurrentTag([...currentTag, event.target.value]);
  };

  console.log({ currentTag });
  return (
    <div>
      <select onChange={event => tagSelector(event)}>
        <option>1</option>
        <option>2</option>
        <option>3</option>
      </select>
      <DisplayOption {...{ currentTag, setCurrentTag }} />
    </div>
  );
}

function DisplayOption(props) {
  const { currentTag, setCurrentTag } = props;

  if (isEmpty(currentTag)) return null;

  return currentTag.map((each, index) => (
    <span key={index} className="tag" style={{ backgroundColor: "red" }}>
      <span
        className="tag is-link"
        style={{ marginLeft: ".5em", fontSize: "16px", fontColor: "red" }}
      >
        {each}
      </span>
      <div
        href=""
        className="tag is-delete"
        onClick={() => removeTags(each, currentTag, setCurrentTag)}
      />
    </span>
  ));
}

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

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