我确定这只是语法问题。如何在无状态函数表达式中console.log
?
const Layer = (props) => (
console.log(props); //breaks
)
没有必要更改组件的结构以使用花括号并添加返回值。您可以这样做:
const Layer = (props) => console.log(props) || (
...whatever component does
);
之所以中断,是因为您隐式返回了console.log
,并且您应该返回一些有效的jsx内容。
添加大括号并显式返回该组件:
const Layer = (props) => {
console.log(props);
return <div/> //return a valid React component
};
const StatelessComponent = props => {
console.log(props);
return (
<div>{props.whatever}</div>
)
}
请记住,在功能组件中,照原样存在no渲染方法。您的JSX应该写在函数的return部分中。那不是反应的具体情况。这就是箭头函数本身的行为。祝你好运编码:)