简体   繁体   English

如何使用curring有条件地将数据传递给函数

[英]How to conditionally pass data into a function using currying

I'm somewhat new to functional programming. 我对函数式编程有些陌生。 Here is my current paradigm from my React app. 这是我当前的React应用范例。 I have a generic component into which I'm passing a function which can optionally take a placeholder argument. 我有一个通用组件,要向其中传递一个函数,该函数可以选择使用placeholder参数。

<TableColumn field="effectiveDate" format={dateFormatter} sortable>Start Date</TableColumn>
<TableColumn field="endDate" format={dateFormatter("No End Date")} sortable>End Date</TableColumn> 

I believe I need to use currying to acheive my goal (which I'll speak to shortly). 我相信我需要用粗俗的话来实现我的目标(稍后我会讲)。 That being the case, I've defined dateFormatter like this: 既然如此,我就这样定义了dateFormatter

export function dateFormatter(placeholder?: string) {
  return function (date: string) {
    const attributes: any = { value: date };
    if (placeholder) {
      attributes.placeholder = placeholder;
    }
    return <ShortDate {...attributes} />;
  };
}

Then, when I actually render my table, I loop through each record and get the value like this: 然后,当我实际渲染表时,我遍历每条记录并获得如下值:

getRecordValue(record: any, column: React.ReactElement<TableColumnProps>) {
    const { format, field } = column.props;
    const cell = field ? record[field] : "";
    if (format) {
      return format()(cell, record);
    }
    return cell;
}

I want to have a single dateFormatter function, but be able to optionally pass a placeholder and defer execution. 我希望有一个dateFormatter函数,但可以选择传递一个placeholder并推迟执行。 How can I do this? 我怎样才能做到这一点?

just use props. 只是使用道具。 Check if the parrent has passed certain props, in your case is format . 检查您的父母是否通过了某些道具,在您的情况下为format

class App extends React.component{
  render(){
     <CustomComponent  format={dateFormatter("No End Date")}/>
  }
}

class CustomComponent extends React.component{
  constructor(props){
    super(props)
  }

  render(){
     <div>
       Your content maybe
       { this.props.format && this.props.format }
     </div>
  }
}

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

相关问题 javascript function 柯里化,传递一个 function 传递更高的 ZC1C425268E68385D1AB507 参数 - javascript function currying, pass a function that passes the higher function parameters with currying 如何在javascript中使用curring编写具有无数个参数的sum函数? - how to write a sum function with infinite number of arguments, using currying in javascript? 如何在react js中有条件地传递数据? - How to conditionally pass data in react js? 使用lodash调用翻转函数-有限制吗? - Currying a flipped function using lodash - Are there limitations? 如何使用currying函数删除事件侦听器 - How to remove Event listener with currying function 如何使Javascript与curring和函数组成协调一致 - How to reconcile Javascript with currying and function composition 如何使用原型,IIFE,currying或使用new关键字在JavaScript中创建自定义可链接延迟函数 - How to create a custom chainable delay function in JavaScript using prototype, IIFE, currying or using new keyword 如何捕获函数中需要的参数数量(循环函数部分应用程序) - how capture the arguments number need in a function (currying function partial application) 当使用.bind()进行函数调用时,此变量的作用是什么? - What is the role of the this variable when it comes to function currying using the .bind()? js currying函数示例 - js currying function example
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM