3

我正在为 WordPress 编写一个古腾堡块插件,但我无法理解新的 ESNext 语法。我有一个带有键:值配对的字段列表。这是我正在工作的代码:

{
  ...
  edit: function( { className, attributes, setAttributes } ) {
    return (
      <div className={ className }>
        <label className="field">
          <span className="field__label">Name</span>
          <PlainText
            className="field__input"
            id="email"
            value={ attributes.name }
            onChange={ ( name ) => setAttributes( { name } ) }
          />
        </label>
        <label className="field">
          <span className="field__label">Email</span>
          <PlainText
            className="field__input"
            id="email"
            value={ attributes.email }
            onChange={ ( email ) => setAttributes( { email } ) }
          />
        </label>
      </div>
    );
  }
  ...
}

我希望这段代码减少重复并避免复制粘贴。这是我走了多远:

{
  edit: function( { className, attributes, setAttributes } ) {
    var fields = {
      name:    'Name',
      email:   'Email',
      phone:   'Phone',
      website: 'Website',
    }
    var html = [];
    for ( var field_key in fields ) {
      var label = fields[ field_key ];
      html.push( (
        <label className="field" key={field_key}>
          <span className="field__label">{ label }</span>
          <PlainText
            className="field__input"
            id={field_key}
            value={ attributes[field_key] }
            onChange={ ( ??? ) => setAttributes( { ??? } ) }
          />
        </label>
      ) );
    }
    return (
      <div className={ className }>
        {html}
      </div>
    );
  },
}

如何将field_key变量传递给setAttributes函数?

4

2 回答 2

2

您实际上并不需要该handler函数来绑定field_key。如果您只是将箭头函数传递给onChange它将绑定field_key,然后您可以将其传递给setAttributes

for (const field_key in fields) {
  const label = fields[field_key];

  html.push(
    <label className="field" key={field_key}>
      <span className="field__label">{ label }</span>
      <PlainText
        className="field__input"
        id={field_key}
        value={ attributes[field_key] }
        onChange={() => { setAttributes({ [field_key]: fields[field_key] }); }}
       />
    </label>
  ); 
}

我在Codepen上放了一个示例来说明这一点(React 示例,但也应该适用于您的情况)。

于 2018-07-31T16:21:39.097 回答
0

我找到了解决方案。到onChange触发时,for loop已完成并field_key引用最后一个键“网站”。为了解决这个问题,我为处理函数创建了一个闭包,该闭包将引用锁定在field_key当前迭代中。

const handler = (setter, key) => value => setter( { [key]: value } );
{
  ...
  edit: function( { className, attributes, setAttributes } ) {
    let fields = {
      name:    'Name',
      email:   'Email',
      phone:   'Phone',
      website: 'Website',
    }
    let html = [];
    for ( let field_key in fields ) {
      let label = fields[ field_key ];
      html.push( (
        <label className="field" key={field_key}>
          <span className="field__label">{ label }</span>
          <PlainText
            className="field__input"
            id={field_key}
            value={ attributes[field_key] }
            onChange={handler( setAttributes, field_key )}
          />
        </label>
      ) );
    }
    return (
      <div className={ className }>
        {html}
      </div>
    );
  },
  ...
}
于 2018-07-31T13:11:22.117 回答