2

给定以下组件。我正在寻找一种方法来获取这些道具、属性并将它们以这种格式传递给另一个组件。

<AdminHeader
  logoSource='https://dummyimage.com/85x85/c718de/ffffff'
  leftStyle={{flexGrow: 2}}
  centerText={'Painel De Administração · Clientes'}
  rightStyle={{flexBasis: 'content', flexGrow: 0}}
  right={(
    <AdminNotification
      imageSource='https://dummyimage.com/65x85/a8a8a8/000000.png'
      circleScheme='red'
      count={21}
      text='Admin Master'
    />
  )}
/>

例如,假设我<AdminHeader/>这样包装:

function WrappedAdminHeader (props) {
  return (
    <AdminHeader {...props.adminHeader}/>
  )
}

然后我想调用<WrappedAdminHeader />并传递adminHeader道具,而不必将它们全部转换为JSON

<WrappedAdminHeader
  adminHeader={(
    <ResolveToJSON
      logoSource='https://dummyimage.com/85x85/c718de/ffffff'
      leftStyle={{flexGrow: 2}}
      centerText={'Painel De Administração · Clientes'}
      rightStyle={{flexBasis: 'content', flexGrow: 0}}
      right={(
        <AdminNotification
          imageSource='https://dummyimage.com/65x85/a8a8a8/000000.png'
          circleScheme='red'
          count={21}
          text='Admin Master'
        />
      )}
    />
  )}
/>

而不必像这样将属性转换为 JSON:

<WrappedAdminHeader adminHeader={{
  logoSource: 'https://dummyimage.com/85x85/c718de/ffffff',
  leftStyle: {flexGrow: 2},
  centerText: 'Painel De Administração · Clientes',
  rightStyle: {flexBasis: 'content', flexGrow: 0},
  right:(
    <AdminNotification
      imageSource='https://dummyimage.com/65x85/a8a8a8/000000.png'
      circleScheme='red'
      count={21}
      text='Admin Master'
    />
  )}}
}>

这可能吗?

4

2 回答 2

0

你的意思是这样的?

const A = ({ message }) => <span>{message}</span>;
const B = ({ propsForA }) => <A {...propsForA} />;

const a = <A message="Hi!" />;    

const App = () => (
  <div>{<B propsForA={a.props} />}</div>
);

ReactDOM.render(
  <App />,
  document.getElementById("entry")
);

您从中获取道具的地方aA.

于 2017-03-02T20:11:45.257 回答
0

如果你的意思是你想要<WrappedAdminHeader>传递给<AdminHeader>你的属性,你可以直接在道具上使用属性传播:

function WrappedAdminHeader (props) {
  return (
    <AdminHeader {...props}/>
  )
}

现在,您可以将<WrappedAdminHeader>其用作以下的替代品<AdminHeader>

<WrappedAdminHeader
  logoSource='https://dummyimage.com/85x85/c718de/ffffff'
  leftStyle={{flexGrow: 2}}
  centerText={'Painel De Administração · Clientes'}
  rightStyle={{flexBasis: 'content', flexGrow: 0}}
  right={(
    <AdminNotification
      imageSource='https://dummyimage.com/65x85/a8a8a8/000000.png'
      circleScheme='red'
      count={21}
      text='Admin Master'
    />
  )}
/>
于 2017-03-02T20:02:43.077 回答