redux react-native -为什么必须刷新才能查看更新映像的结果

ulmd4ohb  于 2022-11-24  发布在  React
关注(0)|答案(1)|浏览(149)

我还是一个react的初学者。现在,我正在学习react-native,我试着上传一张照片,但是屏幕上只显示了来自redux的每个数据的undefined值。
useEffect有什么问题吗?我想是关于useEffect的,我希望有人能帮助我。下面是代码:

import { getUserById, updateImageUser } from '../../stores/actions/user';
import { launchCamera, launchImageLibrary } from 'react-native-image-picker';

const editProfile = props =>{
 const user = useSelector(state => state.user);

  const requestCameraPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.CAMERA,
        {
          title: 'Cool Photo App Camera Permission',
          message:
            'Cool Photo App needs access to your camera ' +
            'so you can take awesome pictures.',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('You can use the camera');
        const result = await launchCamera({
          selectionLimit: 1,
        });

        const formData = new FormData();
        formData.append('image', {
          name: result.assets[0].fileName,
          type: result.assets[0].type,
          uri: result.assets[0].uri,
        });
        const userId = await AsyncStorage.getItem('userId');
        const data = await dispatch(updateImageUser(userId, formData));
        // console.log(data);
      } else {
        console.log('Camera permission denied');
      }
    } catch (err) {
      console.warn(err.response);
    }
  };

return (
  <View style={styles.containerPhoto}>
    <TouchableOpacity
      onPress={() => requestCameraPermission()}
      style={styles.rounded}>
      {user.data?.image ? (
        <ImageBackground
          source={{
            uri: `https://res.cloudinary.com/atma-jaya-yogyakarta/image/upload/v1669068823/${user.data.image}`,
          }}
          style={{resizeMode: 'cover', width: '100%', height: '100%'}}
          borderRadius={50}
        />
      ) : (
        <Image source={require('../../assets/Avatar.png')} />
      )}
    </TouchableOpacity>
    <Text style={styles.name}>
      {user.data.name ? user.data.name : 'empty'}
    </Text>
    <Text style={styles.role}>
      {user.data.profession ? user.data.profession : 'empty'},{' '}
      {!user.data.nationality ? 'empty' : user.data.nationality}
    </Text>
  </View>
);

值成功,但屏幕还是没有变化,我得手动刷新,请帮帮忙,已经花了我几个小时了。
谢谢大家!

myzjeezk

myzjeezk1#

所以在我的例子中,我有redux操作getUserById(userId)。所以我只是在下面的代码const data = await dispatch(updateImageUser(userId,formData))中再次调用它。所以函数将重新获取数据

相关问题