简体   繁体   English

Render函数中意外的令牌返回。 反应本机

[英]Unexpected token return in Render function . React Native

Getting "Unexpected token" on return in render 渲染返回时获取“意外令牌”

render() {
   return (

Cannot understand why this is happening. 无法理解为什么会这样。 I have curly braces in the function inside render ?! 我在render函数里面有花括号吗?

export class NoteList extends React.Component {
constructor(props) {
   super(props);
   state = {cnt: 0}
}

componentDidMount() {
   this.state.cnt = 0;
}

appendNoteToDiagram = (note, index) => {

   this.state.cnt++;
   let xpos = this.state.cnt;
   let ypos = this.state.cnt * 60;
   return (<Note x-position="{xpos}" y-position="{ypos}" width="100" 
            height="50" stroke-color="red" fill-color="red" text=" 
            {note.text}">);
 }

 render() {
   return ( // <<<<<< Error Here
       { this.props.notes.map((note, index) => (
           return appendNoteToDiagram(note, index)
       ))}
   );
 }
}

Put this JSX inside div. 将此JSX放在div中。

<div>
{ this.props.notes.map((note, index) => (
       return appendNoteToDiagram(note, index)
   ))}
</div>

You're using ( instead of { , which doesn't make sense because you're calling a function. Ideally, because of appendNoteToDiagram function signature, I'd change this: 您正在使用(而不是{ ,这没有意义,因为您正在调用函数。理想情况下,由于appendNoteToDiagram函数签名,我将对此进行更改:

  return (
       { this.props.notes.map((note, index) => (
           return appendNoteToDiagram(note, index)
       ))}
   );

To this: 对此:

  return (<>{this.props.notes.map(appendNoteToDiagram))}</>)

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

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