Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native:如何将构造函数与功能组件结合起来?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 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(){ 返回( ... ) } } 我看到在较

到目前为止,在React Native中,我总是创建这样的组件

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
}, []);