【问题标题】:How can I pass a multidimensional array as props in React?如何在 React 中将多维数组作为道具传递?
【发布时间】:2020-04-12 23:29:58
【问题描述】:

我需要将数组数组传递给 React 16.12 中的子组件。我不希望将数组分解为父级中的元素(仍然是数组)并将其作为一个整体传递。但似乎 React 将内部数组转换为字符串。 我使用以下场景来解释这个案例,这不是我的应用所做的,就是这样:

  1. 这是我想要传递给孩子的数组 -> [[0, 0], [100, 0], [100, 100], [0, 100]]

  2. 这是我记录子道具时浏览器(firefox、chrome)返回的内容 -> [[0, 0], [100, 0], [100, 100], [0, 100]]

似乎一切都很好......

  1. 这是我想在屏幕上显示道具内容时的代码和渲染结果 ->
    class Rectest extends React.Component {
        constructor(props) {
            super(props);
        }
        render() {
            console.log(this.props)
            return (
                <div>
                    <h6>
                        {this.props.corners[0]}
                    </h6>
                    <h6>
                        {this.props.corners[1]}
                    </h6>
                    <h6>
                        {this.props.corners[2]}
                    </h6>
                    <h6>
                        {this.props.corners[3]}
                    </h6>
                </div>
            )
        }
    }


道具日志-> [[0, 127 ], ​​ [128, 0 ], ​​[4, 133 ], ​​[4, 127 ]]

h6标签中渲染的内容-> 0127 1280 4133 4127

看起来内部数组在渲染函数中被视为字符串...

  1. 当我使用这段代码时 ->
    class Rectest extends React.Component {
        constructor(props) {
            super(props);
        }
        render() {
            console.log(this.props)
            return (
                 <div>
                    <h6>
                        {this.props.corners[0][0]}
                    </h6>
                    <h6>
                        {this.props.corners[1]}
                    </h6>
                    <h6>
                        {this.props.corners[2]}
                    </h6>
                    <h6>
                        {this.props.corners[3]}
                    </h6>
                </div>
            )
        }

    }

我收到一个类型错误:TypeError: this.props.corners[0] is undefined

最后,这是父组件中的子组件:

<Rectest corners={this.state.photoCorners}/>

而 this.props.corners[0] 似乎是一个数组,因为这段代码:

console.log(Array.isArray(this.props.corners[0]))

返回真。

【问题讨论】:

  • 好的,但是为什么要在h6标签内显示数组元素呢?
  • @Baran Nazari 你想做什么?有什么要求
  • 我不想给他们看!我想把案子说清楚。 @Ramesh
  • 我想根据道具画一个矩形。 @Vahid Akhtar
  • 但是可能的解决方案对我来说比这种情况有用得多。正如我所提到的,有一些方法可以打破数组并单独发送内部部分。我只是做了这个简单的场景来说明这一点,我希望它不会造成更多的混乱:)

标签: reactjs state react-props


【解决方案1】:

检查我的代码笔 - https://codepen.io/viciousandambitious/pen/VwYpBjr?editors=1111 - 您提供的代码似乎一切正常。您可能在道具中传递了错误的值。请确保您传递的是一个数组。

class Rectest extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        console.log(this.props.corners[0])
        return (
            <div>
                <h6>
                    {this.props.corners[0][0]}
                </h6>
                <h6>
                   {this.props.corners[0][1]}
                </h6>
                <h6>
                    {this.props.corners[1][0]}
                </h6>
                <h6>
                    {this.props.corners[1][1]}
                </h6>
            </div>
        )
    }
}

React.render(<Rectest corners={[[123, 123], [234, 435]]}/>, document.getElementById('app'));

【讨论】:

  • 谢谢@viciousP 我看到了你的代码笔,但我的游乐场似乎以某种方式运作不同。我也试过你的方法。
  • @BaranNazari 好的,你能解释一下不同之处,以便我们提出适当的解决方案吗?
  • @viciousP 你是说我的环境吗?
  • @BaranNazari 是的,作为示例,您提出的问题似乎工作正常。我想知道这与您的“真实”示例有什么区别。
  • @viciousP 我想他只是想知道为什么他的数组元素显示为字符串而不是控制台中显示的那样(带方括号)。
猜你喜欢
  • 2018-08-01
  • 2017-09-06
  • 2020-10-19
  • 2020-08-20
  • 2023-03-28
  • 2018-08-22
  • 2020-07-12
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多