简体   繁体   English

将字符串文字内的函数传递给其他组件

[英]Pass function inside string literal to other component

I want to pass a function defined in my actions to an element. 我想将动作定义的函数传递给元素。

Reducer 减速器

case 'UPDATE_HEADER':
    return Object.assign({}, state, {
        headerChildren: state.headerChildren.concat([action.child])
    });

Action.js 动作.js

export const deleteHeader = () => {
    return {
        type: 'DELETE_HEADER',
    };
}

I am passing this action to my sidebar 我正在将此操作传递到侧边栏

onAddHeader () {
    this.props.addHeader();
    this.props.updateHeader(
        `<Header key='${this.props.childKey}' 
             deleteHeader='${this.props.deleteHeader}'/>`,
    );
}

The deleteHeader function is being passed to the Header component but the problem is that it is being passed as a string and not as a function. deleteHeader函数正在传递给Header组件,但问题在于它是作为字符串而不是作为函数传递的。

I am rendering it as this 我正在渲染它

`{this.props.headerChildren.map((value, index) => (
    <JsxParser
        key={index}
        components={[Header]}
        jsx={value}
    />
))}`

Can someone please guide me through the solution. 有人可以指导我完成解决方案。 I have searched a lot and couldn't find a way to do it. 我已经搜索了很多,却找不到方法。 If this is not the correct approach to do so, kindly let me know the proper way to do it. 如果这样做不是正确的方法,请告诉我正确的方法。 I am new to React + Redux so finding it a bit difficult. 我是React + Redux的新手,所以觉得有点困难。

Thanks in advance 提前致谢

I am going to take a stab at guessing what I think you want to achieve here - it is a bit unclear so if I am wrong please feel free to comment and I can try and fix. 我要猜测一下我认为您想在此处实现的目标-尚不清楚,因此,如果我做错了,请随时发表评论,我可以尝试解决。

You don't need to use a string template if you are not trying to pass a string. 如果您不尝试传递字符串,则无需使用字符串模板。 You can simply pass the component and props themselves. 您可以简单地传递组件并自行支撑。 Something like this... 像这样

// I'm assuming you're doing something like this 
import Header from './wheverYourHeaderIs';

...

onAddHeader () {
    this.props.addHeader();
    this.props.updateHeader(Header, 
       { key: this.props.childKey, 
         deleteHeader: this.props.deleteHeader 
       }
    );
}

I turned your props into 1 keyed object so it is flexible to add more/less props without having to modify the number of arguments your updateHeader function has to take. 我将您的道具变成了1个键对象,因此可以灵活地添加更多/更少的道具,而无需修改updateHeader函数必须采用的参数数量。

Then your updateHeader would do something like : 然后,您的updateHeader将执行以下操作:

 function updateHeader(Component, props) {

  // do whatever you were going to do with your component
     <Component {...props} />
  // becasue we combined props into one object 
  // this is the same as <Header key={this.props.childKey} deleteHeader={this.props.deleteHeader}/> 
 }

Not sure if this is what you were trying to achieve please feel free to comment if not. 不确定这是否是您要实现的目标,如果没有,请随时发表评论。

You don't need to use $ or single quotes when passing props these kind of props.. You just need to enclose the value to be evaluated in {}.. It is only when you need to pass a string literal do you use single quotes. 在传递这类道具时,您不需要使用$或单引号。.您只需要将要计算的值放在{}中。仅当您需要传递字符串文字时,才使用single引号。

So use something like this instead 所以改用这样的东西

<Header key={this.props.childKey} deleteHeader={this.props.deleteHeader}/>

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

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