![](/img/trans.png)
[英]What is solution for constructor in functional components in react native?
[英]React Native: how do you include constructor with functional components?
到目前為止,在 React Native 中,我總是創建這樣的組件
class <name> extends Component {
constructor(props) {...}
componentDidMount() {...}
render() {
return (
...
)
}
}
我看到在較新的文檔中,他們創建了這樣的組件:
function <name> {
return (
...
)
}
如果我使用第二種語法,如何添加constructor
和componentDidMount
?
如 react 的 react 鈎子文檔中所述,您可以使用useEffect鈎子並傳入一個空數組作為依賴數組,
如果你想運行一個效果並且只清理一次(在掛載和卸載時),你可以傳遞一個空數組([])作為第二個參數。 這告訴 React 你的效果不依賴於 props 或 state 的任何值,所以它永遠不需要重新運行。
useEffect(() => {
// insert logic for componentDidMount here
}, []);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.