0

I'm try to update userProfile image but getting that error

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

here is my code

class Profile extends Component {
 state = {
 userImage: require("../assets/cena.jpg"),
 userName: "John Cena",
 userInfo: "The champ is here",
 modal: false,
 image: null,
 hasCameraPermission: null,
 };
 async componentDidMount() {
 const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
 this.setState({ hasCameraPermission: status === "granted" });
 }
 pickImage = async () => {
 let result = await ImagePicker.launchImageLibraryAsync({
 mediaTypes: ImagePicker.MediaTypeOptions.Images,
 allowsEditing: true,
 aspect: [4, 3],
 });
 if (!result.cancelled) {
 this.setState({ image: result.uri });
 this.props.formikProps.setFieldValue("image", result.uri);
 }
 };
 closeModal = () => {
 this.setState({ modal: false });
 };
 render() {
 
 return (
 <SafeAreaView style={styles.screen}>
 <View style={styles.container}>
 <TouchableOpacity
 activeOpacity={0.7}
 onPress={this.pickImage}
 style={styles.TouchableOpacityStyle}
 >
 <MaterialCommunityIcons
 name="camera"
 size={30}
 color={colors.white}
 />
 </TouchableOpacity>
 {!this.state.image && (
 <Image style={styles.image} source={this.state.userImage} />
 )}
 {this.state.image && (
 <Image
 style={styles.edit}
 source={this.setState({ userImage: this.state.image })}
 />
 )}

can someone tell me what's going on?

asked Oct 3, 2020 at 9:20

2 Answers 2

0

You can simply do it like this :

 {this.state.image ? (
 <Image style={styles.image} source={this.state.userImage} />
 ) : (
 <Image
 style={styles.edit}
 source={this.state.userImage}
 />
 )}

You can also use the onError prop of image component to change the source prop.

answered Oct 3, 2020 at 11:25
Sign up to request clarification or add additional context in comments.

4 Comments

Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_this.props.formikProps.setFieldValue') where should i use setstate?
What is the purpose of changing state?
The error is in your formik line, comment it out and try to run the code.
i removed it but it's not setting userImage to image i choose from gallery bcoz i need to use setState, it's so easly in function base
0

remmove the setState as it should be anonymous otherwise will call on every render

 {this.state.image && (
 <Image
 style={styles.edit}
 source={this.setState({ userImage: this.state.image })}
 />
 )}

should be

 {this.state.image && (
 <Image
 style={styles.edit}
 source={this.state.image}
 />
 )}

by the way you can't setState in the source prop as it only be given the picture path !

answered Oct 3, 2020 at 9:44

4 Comments

so how can I correct it I'm familiar with function base but never done this in class base, can you help?
Failed prop type: Invalid prop source` supplied to Image.` got that error and that warning promise rejection: TypeError: undefined is not an object (evaluating '_this.props.formikProps.setFieldValue
comment that formik line.
i removed it but it's not setting userImage to image i choose from gallery bcoz i need to use setState, it's so easly in function base

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.