【问题标题】:How to modify an array and add new element to array state on react如何在反应时修改数组并将新元素添加到数组状态
【发布时间】:2021-10-17 11:21:53
【问题描述】:

我是基于函数(无类)的 React 新手,尝试使用 state redux 从组件中获取数组,并将其添加到新数组中,因为我只需要两个属性即可将元素添加到多个 Select 组件. 我尝试了不同的东西,但没有运气。我会接受任何帮助。这是我的代码:

const FormInfo = () => {

    //reading an array from another component
    const arrayFromOtherComponent = useSelector(state => state.otherComponente.array);

    //creating a local state with empty array
    const [state, setState] = useState([]);

    //creating new state to add this object to the array state
    const [ item, setItem ] = useState({
        value: '',
        label: ''
    })

    //looping arrayFromOtherComponent to get some properties and add to state
    const callSelect = () => {
        arrayFromOtherComponent.map( var => (
            setItem({
                value: '`{var.id}`',            -> I cant' read the var object
                label: '`{var.name}`'           -> I cant' read the var object
            }),
            setState( state.concat(item) )      -> Is not updating the array state
        ));
    }

    //Calling function to update array
    useEffect ( () => {
        if(arrayFromOtherComponent){
            callSelect() }
    }, []);
    .
    .
    .

    //Using a Multiple Select to return the info
    return (
        .
        .
        .
            <Select
                isMulti
                options={state}       -> this should iterate the array state to show options
                value={state.selectedOption}
                onChange={handleChange}
                closeMenuOnSelect={false}
            />
        .
        .
        .

    );
}
export default FormInfo;

【问题讨论】:

    标签: arrays reactjs select state element


    【解决方案1】:

    callSelect 方法可能是这样的:

    const callSelect = () => {
        const initialArray = arrayFromOtherComponent.map((element) => {
          return {
            value: element.id,
            label: element.name
          };
        });
        setState(initialArray);
      };
    

    【讨论】:

    • 谢谢你,Anass,这行得通。你教会了我另一种我没有考虑过的方法。最好的问候!!
    • 不客气@DiegoBenedetto 祝你好运。
    【解决方案2】:
    const callSelect = () => {
        arrayFromOtherComponent.map( var => (
            setItem({
                value: `${var.id}`,            //Use $ before {} and just inside `` not inside '``'
                label: `${var.name}`           
            }),
            setState( state.concat(item) )      -> Is not updating the array state
        ));
    }
    

    请详细说明您想要实现的目标。

    【讨论】:

    • 非常感谢 Sanskar,但仍然无法正常工作。我正在尝试添加更多代码,但我收到此消息“您的帖子似乎包含格式不正确的代码”。这是我的第一篇文章,对不起。同时我可以告诉你,即使我使用这个: SetItem({ value: 'a', label: 'a'}) "item" 和 "state" 这样仍然是空的: item: {"value": "","label":""} state: [] 我通过控制台日志看到了这个
    • 谢谢Sanskar,你教我如何分配值:${var.id},也许我的逻辑代码现在不起作用,但它对我将来非常有用。
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2020-12-03
    • 2021-10-23
    • 2019-06-16
    • 2018-05-03
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多