为什么我的裁判在React Native中不起作用?

仙女皇后

我正在使用React 16.3.0-alpha.1,并且试图创建一个引用以检查是否已安装我的组件,以避免出现“只能更新已安装或正在安装的组件”警告。我正在检查我的componentDidMount函数中的ref,但是它对于ref永远没有任何值,因此它永远不会进入if语句。有谁知道谁让裁判上班呢?

constructor(props) {
    super(props);

    this.productCard = React.createRef();
}

componentDidMount() {
    this.props.dispatch(handleLoadProduct(this.props.product.id)).then((data) => {
        if (data.response && data.response.images) {
            let images = data.response.images.map(img => {
                return 'https://b2b.martinsmart.com/productimages/' + img.original;
            });

            if (this.productCard) {
                this.setState({imgArray: images});    
            }
        }
    });
}

和视图:

<View
  ref={(pc) => this.productCard = pc}
  style={{height: '100%', backgroundColor: '#D6D6D6'}}
>
托勒

可以使用常规实例变量ref代替使用a来检查组件是否在使用前仍然挂载setState

class App extends React.Component {
  mounted = true;

  componentDidMount() {
    this.props.dispatch(handleLoadProduct(this.props.product.id)).then(data => {
      if (!this.mounted) {
        return;
      }

      if (data.response && data.response.images) {
        let images = data.response.images.map(img => {
          return "https://b2b.martinsmart.com/productimages/" + img.original;
        });

        this.setState({ imgArray: images });
      }
    });
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  // ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

react native 为什么我的条件不起作用?

clearTimeout在React Native中不起作用

订阅在React Native中不起作用?

flex在react-native中的ScrollView中不起作用

为什么访问调用元素的道具在 React Native 中不起作用

为什么大纲 css 属性在 react-native 中不起作用?

为什么animation-name属性在React Native中不起作用?

为什么 react-native 中的 GET 请求不起作用?

为什么 svg 文件在 react-native 中不起作用?

React Native,为什么secureTextEntry不起作用?

React Native:在TestFlight中显示图像不起作用

React Native:iOS中的透明Stack Navigator不起作用

React Native 中的垂直居中不起作用

clearInterval() 在 React Native 功能组件中不起作用

useScrollToTop 在 useEffect 中不起作用?(React Native Expo)

posesEnabled:false在React Native中不起作用

获取在 React Native ios 中不起作用

REACT NATIVE - this.setState 在 onChangeText 中不起作用

在 React Native fetch 方法中解析 JSON 不起作用

视图样式在 React Native 中不起作用

onPress在React Native Flatlist中不起作用

React Native:NavigationOptions中的标题样式不起作用

Tab 中的 react-native-maps 不起作用

连接分派在React Native中不起作用

开关盒在React Native中不起作用

图像resizeMode ='contain'在React Native中不起作用

PushNotificationIOS.presentLocalNotification()在react-native中不起作用

onNavigationStateChange在Android React Native中不起作用

Axios发布请求在React Native中不起作用