为什么要在无状态函数表达式中破坏'console.log'?

问题描述 投票:4回答:3

我确定这只是语法问题。如何在无状态函数表达式中console.log

const Layer = (props) => (
  console.log(props); //breaks
)
javascript reactjs syntax components
3个回答
6
投票

没有必要更改组件的结构以使用花括号并添加返回值。您可以这样做:

const Layer = (props) => console.log(props) || (
  ...whatever component does
);

4
投票

之所以中断,是因为您隐式返回了console.log,并且您应该返回一些有效的jsx内容。

添加大括号并显式返回该组件:

const Layer = (props) => { 
  console.log(props);
  return <div/> //return a valid React component
};

2
投票
const StatelessComponent = props => {
   console.log(props);
   return (
      <div>{props.whatever}</div>
   )
}

请记住,在功能组件中,照原样存在no渲染方法。您的JSX应该写在函数的return部分中。那不是反应的具体情况。这就是箭头函数本身的行为。祝你好运编码:)

© www.soinside.com 2019 - 2024. All rights reserved.