React js 中的条件属性

Conditional Attributes in React js

如何在 react.js

中接受条件属性

下面是我的搜索组件,如果传递了 onSubmit 函数,我希望 InputGroup 有一个 onSubmit 属性和一个 onChange 属性如果传递了一个 onChange 函数

class QueryBar extends PureComponent {
  render() {
    const { placeholder, leftIcon, onSubmit, onChange, width } = this.props;
    return (
      <form
        style={{ width }}
        onSubmit={e => {
          e.preventDefault();
          onSubmit(e.target[0].value);
        }}
      >
        <InputGroup
          placeholder={placeholder}
          width={width}
          leftIcon="search"
          rightElement={
            <Button
              type="submit"
              icon={leftIcon}
              minimal={true}
              intent={Intent.PRIMARY}
            />
          }
        />
      </form>
    );
  }
}

QueryBar.propTypes = {
  width: PropTypes.number,
  placeholder: PropTypes.string,
  leftIcon: PropTypes.oneOfType(['string', 'element']),
  onSubmit: PropTypes.func
};

QueryBar.defaultProps = {
  placeholder: 'Search...',
  leftIcon: 'arrow-right',
  width: 360
};
export default QueryBar;

如果 null 不存在,您可以传递 null 即:

<InputGroup
          placeholder={placeholder}
          width={width}
          leftIcon="search"
          onChange={onChangeFn?onChangeFn:null} 
          onSubmit={onSubmitFn ? onSubmitFn : null}
          rightElement={
            <Button
              type="submit"
              icon={leftIcon}
              minimal={true}
              intent={Intent.PRIMARY}
            />
          }
        />

它将确定函数是否存在然后调用函数,否则它什么也不会。

我会这样做:

想法是有一个对象optionalProps,一个空对象,用于任何可能的条件属性,当属性存在时,我们将它添加到对象中,然后,在InputGroup 组件,我们将其应用为 {...optionalProps} ,它将提取任何添加到对象的属性,并且 return 如果为 null,则什么都没有。

我们可以采用另一种方法:onChange={onChange && onChange}

但是,请注意,对于 onChange 不存在的情况,这将 return false 作为值。

  render() {
    const { placeholder, leftIcon, onSubmit, onChange, width } = this.props;
    let optionalProps = {};
    if(onChange){
      optionalProps['onChange'] = onChange;
    }
    if(onSubmit){
      optionalProps['onSubmit'] = onSubmit;
    }

    return (
      <form
        style={{ width }}
        onSubmit={e => {
          e.preventDefault();
          onSubmit(e.target[0].value);
        }}
      >
        <InputGroup
          placeholder={placeholder}
          width={width}
          leftIcon="search"
          {...optionalProps}
          rightElement={
            <Button
              type="submit"
              icon={leftIcon}
              minimal={true}
              intent={Intent.PRIMARY}
            />
          }
        />
      </form>
    );
  }

jsx元素也可以接受对象。初始化一个包含两种情况信息的对象,然后添加一个条件来添加一个函数,如果它存在于传入的 props 中。

render() {
    const { placeholder, leftIcon, onSubmit, onChange, width } = this.props;
    const inputGroupProps = {
    placeholder,
    width,
    leftIcon: 'search',
    rightElement: (
        <Button
        type="submit"
        icon={leftIcon}
        minimal={true}
        intent={Intent.PRIMARY}
        />
      )
    }
    if (onChange) {
    inputGroupProps.onChange = onChange
    }
    if (onSubmit) {
        inputGroupProps.onSubmit = onSubmit
    }
    return (
    <form
        style={{ width }}
        onSubmit={e => {
        e.preventDefault();
        onSubmit(e.target[0].value);
        }}
    >
        <InputGroup {...inputGroupProps} />
    </form>
    );
}

虽然我不推荐它,但从技术上讲两者都可以添加,因为不是从父级传入但已解构的 prop 将是 undefined。我不推荐这个,因为它没有表现力,将来可能会让你感到困惑

<InputGroup
placeholder={placeholder}
width={width}
leftIcon="search"
rightElement={
    <Button
    type="submit"
    icon={leftIcon}
    minimal={true}
    intent={Intent.PRIMARY}
    />
}
onChange={onChange} // will be undefined and have no behavior if parent does not pass an onChange prop
onSubmit={onSubmit} // same for this one
/>