简体   繁体   English

将数组值从一个类传递到另一个类

[英]Passing array value from one class to another

I am new to ReactJs and the issue that I am having is that the array value is not being displayed on the body 我是ReactJs的新手,我遇到的问题是数据值没有显示在正文上

class App extends React.Component{
  render(){
    let arr = ["one", "two"]
    arr.map(n => <Append val={n}/>)

    return
       <div></div>
  }
}

class Append extends React.Component{
  render(){

    return(
     <div>
        {this.props.val}
      </div>
     )

  }
}

ReactDOM.render(<App />,document.getElementById("div"))

My goal is to append to the body the two array values. 我的目标是向身体追加两个数组值。

Well, the div is empty and you are not displaying the result of the map within the div. 好吧,div是空的,你没有在div中显示地图的结果。 So that is why you're not seeing anything. 所以这就是你没有看到任何东西的原因。 One solution is to map append within div. 一种解决方案是在div中映射追加。 Also, if you want to display the div in the next line, make sure to wrap it in a parenthesis. 此外,如果要在下一行显示div,请确保将其包装在括号中。 Otherwise, it will print undefined and ignore the intended return value (the div etc). 否则,它将打印undefined并忽略预期的返回值(div等)。

class App extends React.Component{
  render(){
    let arr = ["one", "two"]
    return (
       <div>{arr.map(n => <Append val={n}/>)}</div>
    );
  }
}

class Append extends React.Component{
  render(){
    return(
     <div>
        {this.props.val}
      </div>
     );
  }
}

ReactDOM.render(<App />,document.getElementById("div"))

You've got two issues here: 你有两个问题:

1 1

The elements you're creating with map need to be moved inside the <div> you're returning. 您使用map创建的元素需要移动您返回的<div>

This expression 这个表达

arr.map(n => <Append val={n}/>)

as a statement has no effect on its own. 声明对其自身没有影响。 The map converts a list of strings into a list of <Append> elements, but it doesn't do anything with them. map将字符串列表转换为<Append>元素列表,但它不会对它们执行任何操作。

2 2

return and <div> need to be on the same line, or you need parentheses like you did in the other render function. return<div>需要在同一行,或者你需要像在其他render函数中那样使用括号。

When you write this, 当你写这个,

return
   <div></div>

automatic semi-colon insertion takes place, and what actually runs is this: 自动分号插入发生,实际运行的是:

return;
   <div></div>

So what you're returning is undefined , not the <div> element you wanted. 所以你要返回的是undefined ,而不是你想要的<div>元素。


So the fixed App class looks like this: 所以固定的App类看起来像这样:

class App extends React.Component{
  render(){
    let arr = ["one", "two"];
    return <div>{arr.map(n => <Append val={n}/>)}</div>;
  }
}
class App extends React.Component{
  render(){
    let arr = ["one", "two"];
   // arr.map(n => <Append val={n}/>); 
    let appendValues = arr.map(n => <Append val={n}/>);

    return
       <div>{appendValues}</div>
  }
}

class Append extends React.Component{
  render(){

    return(
     <div>
        {this.props.val}
      </div>
     )

  }
}

ReactDOM.render(<App />,document.getElementById("div"))

 class App extends React.Component{ render(){ let arr = ["one", "two"]; let numbers = []; arr.map(n => numbers.push(<Append val={n} key={n} />)); return ( <div>{numbers}</div> ); } } class Append extends React.Component{ render(){ return( <div> {this.props.val} </div> ); } } ReactDOM.render(<App />, document.getElementById("div")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.js"></script> <div id="div"></div> 

As have an extra look at the document.getElementById functions, it looks like your not giving it a correct id for a element on the page but rather the name of a div element. 另外看一下document.getElementById函数,看起来你没有为页面上的元素赋予正确的id,而是div元素的名称。

Then have a look at the array.map, .map returns a new array so your App component needs to render that array inside of the element it returns. 然后看一下array.map,。map返回一个新数组,这样你的App组件就需要在它返回的元素内呈现该数组。

I made a small video about your question and I added some small tips if you are interested: https://youtu.be/I9U8I1Yn4c4 我制作了一个关于你的问题的小视频,如果你有兴趣我添加了一些小技巧: https//youtu.be/I9U8I1Yn4c4

Look at this code to get some ideas and have a great day! 看看这段代码来获得一些想法并度过美好的一天!

const React = require("react");
const ReactDOM = require("react-dom");

class App extends React.Component {
  render() {
    let arr = ["one", "two"];
    return <ul>{arr.map(n => <Append key={n} val={n} />)}</ul>;
  }
}

class Append extends React.Component {
  render() {
    return (
      <li>
        {this.props.val}
      </li>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("content"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM