1

im new to react and have an issue i cant figure out

i have an external object with data, and if the info delivered is an array instead of just one string/value i want to render it as a <ul> with the different array positions as <li>.

how can i add the current html element as 2nd parameter to the function?

because right now i cant add "this" as a parameter to pass back the info into the according table cell

Line 15 is the line that delivers an unexpected result...

import React from "react"
import games from "../../assets/games"
import './style.scss';

function SingleGame(props) {

function checkContent(content, tdcell) {

        let listcontent = "<ul>";
        for (let i = 0; i < content.length; i++) {
            listcontent += "<li>" + content[i] + "</li>"
        }
        listcontent += "</ul>"

        tdcell.innerHTML = listcontent;
  
}

return (
    <div className="singlegame" id={games[props.game].name}>
            <div className="singlegame--title">
                {games[props.game].name}
            </div>
            <div className="singlegame--imagecontainer">
                <img
                    className="singlegame--imagecontainer-image"
                    src={games[props.game].image}
                    alt={games[props.game].name}
                />
            </div>
            <table className="singlegame--table">
                <tbody>
                    {Object.keys(games[props.game].details).map(detail => (
                        <tr key={detail}>
                            <th><strong>{detail}</strong></th>
                            <td>{checkContent(games[props.game].details[detail], this)}</td>
                        </tr>                        
                    ))}
                </tbody>
            </table>
    </div>
)
}

export default SingleGame

2 Answers 2

1

In react you don't build up strings of html like that. You just render JSX in structure you want. If you want to put some of that structure in a "function" then you just make a new component.

And when using react functional components, you definitely do not use this.

Try something like this, where you make a new component:

function CheckContent(props) {
  return (
    <ul>
      { props.content.map(item => <li>{ item }</li> }
    </ul>
  )
}

And now you can render that component, and pass it your data as a prop:

<td>
  <CheckContent content={games[props.game].details[detail]} />
</td>
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of doing that, you can add another map to your games[props.game].details[detail], so it will be sth like this

<td>
    <ul>
        {games[props.game].details[detail].map(item => (<li>{item}</li>)}
    </ul>
</td>

1 Comment

but i only want a <ul> if the dataset is a array, most datasets are just a string and i dont want <ul><li>singlestring</li></ul>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.