简体   繁体   English

如何从 React.js 中的数组数据写入元素的属性

[英]How to write an element's attributes from the data of an array in React.js

I am trying to create an input component that receive and add functions to the specific trigger type.我正在尝试创建一个输入组件,用于接收特定触发器类型并将其添加到特定触发器类型。 Here is my code.这是我的代码。 But it doesn't work as my wish.但它并不像我希望的那样工作。 Can Someone help me to achieve this.有人可以帮助我实现这一目标吗?

Main Page主页

import CustomTextInput from "./customTextInput";

const test=()=>{
    alert("test");
}
const test1=()=>{
    alert("test1");
}

const MainPage=()=>{
    return(
        <CustomTextInput
            placeholder="example"
            events={[
                {trigger: "onClick", action: test},
                {trigger: "onKeyUp", action: test1},
            ]}
        />
    )
}

Component (customTextInput.js)组件(customTextInput.js)

const CustomTextInput=(props)=>{
    <input
        type="text"
        placeholder={props.placeholder}
        {
            props.events?.map((event, i)=>{
                return(
                    {eval(event.trigger)=event.action}
                )
            })
        }
    />
}
export default CustomTextInput;

when my code is look like given code, I got this error "Parsing error: Unexpected token, expected '...'"当我的代码看起来像给定的代码时,我得到了这个错误“解析错误:意外的令牌,预期的'...'”

I've tried adding each trigger as each prop, but its very difficult to manage.我试过将每个触发器添加为每个道具,但很难管理。 If I do so, I have to add triggers one by one in my component.如果我这样做,我必须在我的组件中一个一个地添加触发器。 So I need to add map to do this所以我需要添加 map 来执行此操作

It would be better to change the structure you pass the events to the child component, just add the event name as a property name and the event handler as a value.最好更改将事件传递给子组件的结构,只需将event名称添加为属性名称并将事件处理程序添加为值。

Main Page主页

import CustomTextInput from "./customTextInput";

const test=()=>{
    alert("test");
}
const test1=()=>{
    alert("test1");
}

const MainPage=()=>{
    return(
        <CustomTextInput
            placeholder="example"
            events={{
                "onClick": test,
                "onKeyUp": test1,
            }}
        />
    )
}

Then you can use the spread operator to pass it to the input .然后你可以使用spread operator将它传递给input Component (customTextInput.js)组件(customTextInput.js)

const CustomTextInput=(props)=>{
    <input
        type="text"
        placeholder={props.placeholder}
        {
            ...props.events
        }
    />
}
export default CustomTextInput;

If for some reason you want to keep the same structure you need to reduce it in the child component to structure of event name as a property and event handler as a value.如果由于某种原因你想保持相同的结构,你需要在子组件中将它reduce为事件名称作为属性和事件处理程序作为值的结构。

import CustomTextInput from "./customTextInput";

const test=()=>{
    alert("test");
}
const test1=()=>{
    alert("test1");
}

const MainPage=()=>{
    return(
        <CustomTextInput
            placeholder="example"
            events={[
                {trigger: "onClick", action: test},
                {trigger: "onKeyUp", action: test1},
            ]}
        />
    )
}

Component (customTextInput.js)组件(customTextInput.js)

const CustomTextInput=(props)=>{
    <input
        type="text"
        placeholder={props.placeholder}
        {
            ...props.events.reduce((acc, item) => {
               return { ...acc, [item.trigger]: item.action }
            }, {})
        }
    />
}
export default CustomTextInput;

 const CustomTextInput = (props) => { return ( <input type="text" placeholder={props.placeholder} {...props.events.reduce((a, b) => ({...a, [b.trigger]: b.action }), {})} /> ); }; export default CustomTextInput;

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

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