Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an array #3470

Closed
RudmanMario opened this issue Mar 20, 2015 · 5 comments
Closed

Display an array #3470

RudmanMario opened this issue Mar 20, 2015 · 5 comments

Comments

@RudmanMario
Copy link

I have a simple problem. I can't find solution for displaying an array of <th> elements. I hava a component with render something like this:

render: function() {
    var thOne = <th ref="one">foo</th>;
    var thTwo = <th ref="two">bar</th>;
    var array = [];
    array.push(thOne);
    array.push(thTwo);
    return (
        array.map(function(cell) {
            return (cell);
        }
    )
}

This type of return doesn't work. Wrapping it in <div> in no good because I'm breaking table structure and then I have lots of warnings. So if I have an array of <th> elements and I don't want to wrap them in

how can I display them?

@bloodyowl
Copy link
Contributor

render itself can output a single element.

you've got 2 solutions :

  • return the cells in the parent tr
  • not making it a composite component, just a function returning a reactFragment
function renderCells(parent, params) {
  return React.createFragment({
    // ref callback API makes it possible to add refs to a parent composite component
    one: <th ref={(c) => parent._one = c}>{params.foo}</th>,
    two: <th ref={(c) => parent._two = c}>bar</th>,
  })
}

// and in the parent component

render() {
  return (
    <tr>
      <td>other td</td>
      {renderCells(this, this.props)}
    </tr>
  )
}

@RudmanMario
Copy link
Author

Thanks for your fast response. I was afraid that it cannot be done like I wanted to. I'll try to implement your solution, thanks.

@iamdustan
Copy link
Contributor

Also want to point out that browsers automatically add tbody’s where necessary which can cause React to stumble. There’s not enough code here to see if you have that issue as well.

@RudmanMario
Copy link
Author

Yes, I've noticed that and in my case there is no problem with that fact. But, thanks for suggestion.

@zpao
Copy link
Member

zpao commented Mar 20, 2015

I'm going to close this and point you to #2127 (feel free to subscribe, discuss more there). This is definitely a known limitation of how React currently works.

@zpao zpao closed this as completed Mar 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants