【问题标题】:Currying React Parent Functions for Child为孩子柯里化 React 父函数
【发布时间】:2021-05-04 22:24:12
【问题描述】:

我正在尝试对父函数进行柯里化,并能够将参数传递给子函数。以下代码对我不起作用。

在父级内:

  public onSavePerson = (person: Person) => value => {
    const result = { ...person, ...value };
    this.props.store.savePerson({ ...person, ...value });
  };

render() {
 .....
   <DropdownButton
     items={statuses}
     itemRender={item => humanizeText(item)}
     onClick={() => this.onSavePerson(row)}
     intentRender={item => getStatusIntent(item)}
     initialText={row.status}
   />
.....

孩子:

export const DropdownButton = props => {
  const { items, itemRender, onClick, intentRender, initialText } = props;
  return (
    <Popover
      content={
        <Menu>
          {items.map((item, i) => (
            <Menu.Item text={itemRender(item)} onClick={onClick(item)} intent={intentRender(item)} key={i} />
          ))}
        </Menu>
      }
      position={Position.BOTTOM}>
      <Button text={humanizeText(initialText)} intent={intentRender && intentRender(initialText)} />
    </Popover>
  );
};

【问题讨论】:

    标签: reactjs currying


    【解决方案1】:

    通过首先调用柯里化函数然后传入参数来解决这个问题

            <Menu>
              {items.map((item, i) => (
                <Menu.Item text={itemRender(item)} onClick={onClick()(item)} intent={intentRender(item)} key={i} />
              ))}
            </Menu>
    
    

    但是,我后来在调用中将其更改为更简单

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 1970-01-01
      • 2021-02-07
      • 2012-10-06
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多