Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React. Creating a function that returns html

I recently started working with react and I am facing a bit of an issue.

Currently I have the following piece of code

<div className="col-md-4"><h4>ML</h4>
{
    game.lines.map(function (lineGroup) {
        return (
            <div className="row">
                <div className="col-md-1">
                    {lineGroup.Pay}
                </div>
                <div className="col-md-3">
                    <strong>{getLineInfo(lineGroup.HomeInfo)}</strong>
                </div>
                <div className="col-md-3">
                    <strong>{getLineInfo(lineGroup.Score)}</strong>
                </div>
                <div className="col-md-3">
                    <strong>{getLineInfo(lineGroup.AwayInfo)}</strong>
                </div>
            </div>
        )
    })
}

This sits in my render() function.

However I have this exact same piece of code copy/pasted 5 more times with only minor changes. I wish to extract it to a function, but I am not sure how would I do this.

Where should I place the function ? -Inside the render() method?

What should I return from it? - A string that contains the html and variables in {} placeholders?

Do I simply call it within the html?

like image 237
Kobek Avatar asked Oct 26 '17 13:10

Kobek


People also ask

Do React components return HTML?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML.

Does React generate HTML?

React will return an HTML string. You can use this method to generate HTML on the server and send the markup down on the initial request for faster page loads and to allow search engines to crawl your pages for SEO purposes.

Does React automatically escape HTML?

React outputs elements and data inside them using auto escaping. It interprets everything inside validationMessage as a string and does not render any additional HTML elements.

Can a function return a component in React?

A function that returns JSX might not be a component, depending on how it is used. To be a component function returning JSX should be used as <Component /> and not as Component() . When a functional component is used as <Component /> it will have a lifecycle and can have a state.


1 Answers

Create function like this :

function gameLines(game) {
    return game.lines.map(function (lineGroup) {
        return (
            <div className="row">
                <div className="col-md-1">
                    {lineGroup.Pay}
                </div>
                <div className="col-md-3">
                    <strong>{this.getLineInfo(lineGroup.HomeInfo)}</strong>
                </div>
                <div className="col-md-3">
                    <strong>{this.getLineInfo(lineGroup.Score)}</strong>
                </div>
                <div className="col-md-3">
                    <strong>{this.getLineInfo(lineGroup.AwayInfo)}</strong>
                </div>
            </div>
        )
    })
}

Use like this :

<div className="col-md-4"><h4>ML</h4>
    { this.gameLines(game) }
</div>

Dont forget to bind the functions

constructor() {
    ...
    this.gameLines = this.gameLines.bind(this);
    this.getLineInfo = this.getLineInfo.bind(this);
}
like image 191
Vivek Doshi Avatar answered Oct 19 '22 14:10

Vivek Doshi