我敢肯定这只是一种语法问题。我如何在无状态函数表达式中使用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>
)
}
请记住,在功能组件中没有渲染方法。您的JSX应该在函数的返回部分中写入。这不是反应具体的情况。这就是箭头功能本身的行为。祝你好运编码:)