console.log在无状态函数表达式中?

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

我敢肯定这只是一种语法问题。我如何在无状态函数表达式中使用console.log

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

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

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>
   )
}

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

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