[英]React EsLint - Component should be written as pure function and after changed it to pure function can't read the property anymore
So I have this simple class before I change it to pure function as eslint told me to do 所以我将这个简单的类改为纯函数,因为eslint告诉我这样做
class user extends Component {
render(){
return(
<Aux>
<UserTable title="User" type="user" role={this.props.location.roleAction}/>
</Aux>
)
}
}
export default user;
and then I got the eslint error said that component should be written as pure function and I try to change that to pure function like down bellow 然后我得到了eslint错误说组件应该写成纯函数,我尝试将其更改为纯函数,如down bellow
const user = () => (
<Aux>
<UserTable title="User" type="user" role={this.props.location.roleAction} />
</Aux>
);
export default user;
and after change it to arrow function, I can't read the this.props.location.roleAction I got an error "cannot read property "location" of undefined " .why is that could happen? 并且在将其更改为箭头功能后,我无法读取this.props.location.roleAction我收到错误“无法读取属性”位置“未定义”。为什么会发生这种情况? any solution to fix the error so I can using the arrow function and able to read the property.
任何修复错误的解决方案,以便我可以使用箭头功能并能够读取属性。 it work fine when I use the previous written component.
当我使用以前的书面组件时,它工作正常。
any help would be really appreciate. 任何帮助都会非常感激。
In the pure function ("Stateless Functional Component" or SFC) form, you receive props as a parameter: 在纯函数(“无状态功能组件”或SFC)表单中,您将接收道具作为参数:
const user = props => ( // <−−−− Here
<Aux>
<UserTable title="User" type="user" role={props.location.roleAction} />
^−−−−−− no `this` here since it's
a parameter
</Aux>
);
This is covered in the docs here . 这将在这里的文档中介绍。 Here's a simple runnable example:
这是一个简单的可运行示例:
const Example = props => ( <div>{props.text}</div> ); ReactDOM.render( <div> <Example text="one" /> <Example text="two" /> </div>, document.getElementById("root") );
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.