小编Nos*_*omo的帖子

如何在 React 的无状态函数组件中初始化类实例?

使用有状态模式,我通常在我的构造函数中初始化一种辅助类,并在一些组件生命周期方法中使用它的方法,如下所示:

class StatefulComponent extends Component {
  constructor(props) {
    super(props);
    this.helper = new HelperClass();
  }

  componentDidMount() {
    this.helper.doSomething();
  }

}
Run Code Online (Sandbox Code Playgroud)

现在,我想将相同的逻辑转换为像这样的无状态函数组件:

const StatelessFunction = (props) => {
   this.helper = new HelperClass();

   useEffect(() => {
     this.helper.doSomething();
   }, []);

}
Run Code Online (Sandbox Code Playgroud)

但是当我看到这个组件从一开始就被每个 prop 更改调用时,我很担心。这让我觉得我的类实例被一遍又一遍地创建。我错了吗?我能做些什么来防止重新创建我的班级并改用 ref 吗?

我遇到了useRef但不确定它是否适合我的情况。

typescript reactjs react-native react-hooks

9
推荐指数
1
解决办法
4851
查看次数

在反应导航中使用 navigation.goBack 发送参数

你好,如何发送导航返回参数?

const Onsubmit = (data, details = null) => {
    console.log(details.formatted_address);
    route.params.onPlaceChosen(
      route.params.id,
      details.formatted_address,
      details.geometry
    );
    navigation.goBack();
  };
Run Code Online (Sandbox Code Playgroud)

这里我想将details.formatted_address中的值传递到页面B。如何?

react-native react-redux react-navigation

6
推荐指数
1
解决办法
1万
查看次数

运行 pod install 后找不到模块 node_modules/@react-native-community/cli/build/bin.js

从一个新项目(npx react-native init MyTestApp)开始,当我运行 npm list @react-native-community/cli 时,我得到:

MyTestApp@4.1.0 /Users/user/Desktop/Projects/MyTestApp
??? (empty)
Run Code Online (Sandbox Code Playgroud)

更大的问题是我无法运行 pod install,我得到:

    throw err;
    ^

Error: Cannot find module '/MyTestApp/node_modules/@react-native-community/cli/build/bin.js
Run Code Online (Sandbox Code Playgroud)

但是,我在 node_modules 中看到 @react-native-community/cli/build/bin.js

作为参考,package.json:

{
  "name": "MyTestApp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/cli": "^4.6.0",
    "react": "16.11.0",
    "react-native": "0.62.1"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.11.0"
  }, …
Run Code Online (Sandbox Code Playgroud)

cocoapods react-native

2
推荐指数
2
解决办法
2325
查看次数