Javascript React Native:如何将构造函数与功能组件结合起来?
到目前为止,在React Native中,我总是创建这样的组件Javascript React Native:如何将构造函数与功能组件结合起来?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,到目前为止,在React Native中,我总是创建这样的组件 class <name> extends Component { constructor(props) {...} componentDidMount() {...} render() { return ( ... ) } } 类扩展组件{ 构造函数(props){…} componentDidMount(){…} render(){ 返回( ... ) } } 我看到在较
class <name> extends Component {
constructor(props) {...}
componentDidMount() {...}
render() {
return (
...
)
}
}
类扩展组件{
构造函数(props){…}
componentDidMount(){…}
render(){
返回(
...
)
}
}
我看到在较新的文档中,他们创建了如下组件:
function <name> {
return (
...
)
}
函数{
返回(
...
)
}
如果我使用的是第二种语法,那么如何添加
constructor
和componentDidMount
?如react的react钩子文档中所述,您可以使用钩子并将空数组作为依赖项数组传入
如果你想运行一个特效并且只清理一次(在安装和
卸载),可以将空数组([])作为第二个参数传递。这
告诉React您的效果不依赖于道具的任何值
或状态,因此它永远不需要重新运行
useEffect(() => {
// insert logic for componentDidMount here
}, []);