passings array as props in reactjs

前端 未结 2 1820
梦谈多话
梦谈多话 2021-02-06 21:02

I am new to react.

I have been experimenting on react and I got stuck on how to pass array using props.

case-1:

var c = [\'program\'];
var Naviga         


        
2条回答
  •  粉色の甜心
    2021-02-06 21:23

    You actually don't need to specify PropTypes at all to use props. It's just a good way to document and verify prop types during development.

    You are using the {} correctly. {} will return the value of the expression inside.

    However, {['everyone']} doesn't make much sense. Here you are asking React to return the value of the array itself, rather than one of the elements/values within the array.

    To get the first value out of your array, you should be doing: {this.props.config[0]} since the value "everyone" is at the 0 index of the array.

    If your array had multiple values, you would do something along the lines of:

    render: function() {
      var values = this.props.config.map(function(value, i){
        return (
          

    value

    ); }); return (
    helloworld {values};
    ); }

    If you truly to mean to actually print out the array itself, and not a particular value within it, you have two good options:

    render: function() {
      return (
        
    helloworld {this.props.config.toString()};
    ); }

    Or

    render: function() {
      return (
        
    helloworld {JSON.stringify(this.props.config)};
    ); }

提交回复
热议问题