如何在 props 数组中水平显示我的值

在我的 React 项目中,我必须从父组件中获取一个数组并将其水平打印在子组件(PrintHorizontal)中。


我正在使用 map 函数来垂直呈现我的值,所以我写了 join 来组合数组值,但我得到了不同的值。打印是我的子组件


props.values=[

   {

    id:1,

    x:1

   },

   {

    id:1,

    x:1

   }

 ]


PrintHorizontal Component:

const PrintHorizontal = props => {  

let list=props.values.map(value => {      

    return <Print key={value.id} data={value.x} />

 });

return (

    <div>

        {list.join(' , ')}

    </div>

)

}


Print Component:

const Print = props => {

    return (

        <div>

            <Avatar>{props.data}</Avatar>  //Avatar is a component imported from materialUI

        </div>

    )

}

我希望运行值会水平打印,而不是我得到 [object Object],[object Object]


倚天杖
浏览 133回答 2
2回答

一只名叫tom的猫

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; main = () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const arr = [1, 2, 3, 4];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const list = arr.map(item => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const node = document.createElement('span');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node.innerHTML = item * 2;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node.style.margin = '20px';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return node;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.forEach(element => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("target").appendChild(element)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script></head><body onload="main()">&nbsp; &nbsp; <div id="target"></div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript