0

I have to files, App.js and Example.js

App.js

import React from 'react'
import Example from './Example'

const App = () => {
    return (
        <div>
            <Example render={ students =>  <ul> Hi , {students.forEach( student => {return <div>{student}</div>}) } </ul>   } />
        </div>
    )
}

export default App

and Example.js

import React from 'react'

function Example(props) {
    return (
        <div>
            {props.render(["Maria", "Alice", "Gina", "Cleopatra"])}
        </div>
    )
}

export default Example

The expected result was ,,Hi Maria , Alice ..." i wanted to see the names, but for some reason, without no error, i only get ,,Hi"

Does anyone see the issue?

Many thanks

2 Answers 2

2

I do not think you should use render props in this case. Also, the method you should use is map, because forEach does not return anything, it simply invokes function on the elements inside the array. Simply pass the students array to Example component and render them inside Example itself. Ex:

<Example  students={//studentsArray} />

then inside Example:

function Example(props) {
    return (
        <div>
            {props.students.map(student => <div>hi {student}</div>)}
        </div>
    )
}
   
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, you where right, when i switched map with forEach, it displayed the values. Many thanks @wyfy !
My reputation is still less then 15, so my vote is not shown, but as soon as i will pass 15, i will come back and vote every answer that helped me!
0

Try using students.map() instead of forEach() function. It works fine for me:

<Example render={ students =>

    Hi , {students.map( student => {return {student}}) }
} />

Comments

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.