Reactjs 有没有办法在React中创建我自己的helper函数?

Reactjs 有没有办法在React中创建我自己的helper函数?,reactjs,Reactjs,在几个组件中,我有一个返回用户化身URL的功能: import defaultAvatar from 'assets/images/default-pic.jpg' class MyComponent extends Component { userAvatar () { const { profile } = this.props if (profile.has_avatar) { return profile.avatar } else {

在几个组件中,我有一个返回用户化身URL的功能:

import defaultAvatar from 'assets/images/default-pic.jpg'

class MyComponent extends Component {
  userAvatar () {
    const { profile } = this.props

    if (profile.has_avatar) {
      return profile.avatar
    } else {
      return defaultAvatar
    }
  }
}

有没有办法在多个组件之间干燥此函数?

如果您使用了
React.createClass
方法,则可以使用mixin跨组件共享代码。因为您使用的是ES6方法,所以可以签出HOC(高阶组件)


参考资料:

如果您使用了
React.createClass
方法,则可以使用mixin跨组件共享代码。因为您使用的是ES6方法,所以可以签出HOC(高阶组件)

参考:

带有默认道具 如果您制作了一个接受头像作为顶级属性的头像组件,那么您可以使用默认道具来指定未提供的值

function Avatar({ avatar }) {
  return <img src={avatar} />;
}

Avatar.defaultProps = { avatar: defaultAvatar };
然后重写原始代码

class MyComponent extends Component {
  userAvatar () {
    const { profile } = this.props

    return getUserAvatar(profile);
  }
}
作为高阶分量 也可以将其作为一个高阶组件来实现

function WithAvatar(Component) {
  return function(props) {
    const { profile } = props;
    const avatar = getUserAvatar(profile);

    return <Component avatar={avatar} {...props} />;
  };
}
function Profile(props) {
  const { profile, avatar } = props;
  return (
    <div>
      <img src={avatar.src} />
      <span>{profile.name}</span>
    </div>
  );
}

const ProfileWithAvatar = WithAvatar(Profile);

render(
  <ProfileWithAvatar profile={exampleProfile} />,
  document.getElementById('app')
);
profile
作为道具传递给外部组件会导致
WithAvatar
对其进行处理并选择正确的化身,然后将其作为道具传递给包装组件。

使用默认道具
function WithAvatar(Component) {
  return function(props) {
    const { profile } = props;
    const avatar = getUserAvatar(profile);

    return <Component avatar={avatar} {...props} />;
  };
}
function Profile(props) {
  const { profile, avatar } = props;
  return (
    <div>
      <img src={avatar.src} />
      <span>{profile.name}</span>
    </div>
  );
}

const ProfileWithAvatar = WithAvatar(Profile);

render(
  <ProfileWithAvatar profile={exampleProfile} />,
  document.getElementById('app')
);
如果您制作了一个接受头像作为顶级属性的头像组件,那么您可以使用默认道具来指定未提供的值

function Avatar({ avatar }) {
  return <img src={avatar} />;
}

Avatar.defaultProps = { avatar: defaultAvatar };
然后重写原始代码

class MyComponent extends Component {
  userAvatar () {
    const { profile } = this.props

    return getUserAvatar(profile);
  }
}
作为高阶分量 也可以将其作为一个高阶组件来实现

function WithAvatar(Component) {
  return function(props) {
    const { profile } = props;
    const avatar = getUserAvatar(profile);

    return <Component avatar={avatar} {...props} />;
  };
}
function Profile(props) {
  const { profile, avatar } = props;
  return (
    <div>
      <img src={avatar.src} />
      <span>{profile.name}</span>
    </div>
  );
}

const ProfileWithAvatar = WithAvatar(Profile);

render(
  <ProfileWithAvatar profile={exampleProfile} />,
  document.getElementById('app')
);

profile
作为道具传递给外部组件会导致
WithAvatar
对其进行处理并选择正确的化身,然后将其作为道具传递给包装组件。

如果可以展平道具,您可以使用
defaultProps
profile
指定默认值,您可以使用
defaultProps
profile
指定默认值。可能值得一提的是,React团队正在将文档和示例转换为使用类,以期最终弃用mixin和
createClass
,因此,HOC可能是一条出路。可能值得一提的是,React团队正在将文档和示例转换为使用类,以期最终反对mixin和
createClass
,因此HOC可能是一条出路。
function WithAvatar(Component) {
  return function(props) {
    const { profile } = props;
    const avatar = getUserAvatar(profile);

    return <Component avatar={avatar} {...props} />;
  };
}
function Profile(props) {
  const { profile, avatar } = props;
  return (
    <div>
      <img src={avatar.src} />
      <span>{profile.name}</span>
    </div>
  );
}

const ProfileWithAvatar = WithAvatar(Profile);

render(
  <ProfileWithAvatar profile={exampleProfile} />,
  document.getElementById('app')
);