【问题标题】:How to pass an Array from Parent Component to Child Component in react by using Statefull Component?如何使用有状态组件将数组从父组件传递给子组件?
【发布时间】:2019-12-31 09:15:15
【问题描述】:

我正在处理反应项目,我正在尝试将数组从父组件传递到子组件以与有状态组件反应。

这是 App.js

import React, { Component } from 'react';
import './App.css';
import Child from './Child/Child'

const students = ['Mark','Williams']


class App extends Component {

render() {
    return (
      <div>
        <Child studentsArray = {students} ></Child>
      </div>
    )
  }
}

export default App

这是 Child.js

import React, { Component } from 'react';
import './Child.css';

class Child extends Component {
render() {
        return (
            <div>

                <ul>
                    {this.props.studentsArray.map(student => `<li> ${student} </li>`)}
                </ul>
            </div>
        )
    }
}

export default Child

【问题讨论】:

  • 你遇到了什么问题?
  • @albert 我的输出是这样的 ->
  • Mark
  • Williams
  • 啊,去掉反引号就可以了
  • 是的,你不需要在这里使用字符串文字。
  • 标签: reactjs


    【解决方案1】:
    class Parent extends Component {
      render() {
        return (
          <>
            <Child studentsArray = {[1,2,3,4,5]} ></Child>
          </>
        );
      }
    }
    class Child extends Component {
    render() {
      return (
          <ul>
           {this.props.studentsArray.map(student =>
            <li> {student} </li>)}
          </ul>
            )
        }
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签