0

I made a form with Formik and react-reaflet, when the user clicks on the button, a map appears with a predefined longitude and latitude and he can navigate on the map and automatically change its location by dragging the cursor.

The functionality works as expected but when I click on the cursor to change the location, React shows me in the console this error Warning: Cannot update a component (Main) while rendering a different component (Formik).. I tried to solve it but it still doesn't work. I created this sandbox so you can see how the error appears on the console

2
  • the link is now dead, so we can no longer see the context for the answer. Can you edit (some of) the code into your question? Commented Aug 19, 2020 at 15:34
  • @Pureferret codesandbox.io/s/lfv2u Commented Aug 19, 2020 at 21:21

2 Answers 2

2

The problem is in components/mapper.js ln:12.

You did:

setLatitude(latitude);
setLongitude(longitude);

right inside a rendering function. Why do you do that?

Sign up to request clarification or add additional context in comments.

3 Comments

I did that because I thought that i could use it as a state in the parent component
It's just not necessary.
thank you so much, i delete that and now it's working without errors
0

Just to generalize what @hackape said, when using functional components, don't use setState plainly on a component's body (you need to initialize the state on definitions or wrap it with useEffect/other callable functions)

Example:

function MyComponent(props){
  const [state, setState] = useState(0)

  setState(5) // This is BAD

  return <View />
}

Solutions:

function MyComponent(props){
  const [state, setState] = useState(5) // This is good

  return <View />
}

// -------- OR ---------
function MyComponent(props){
  const [state, setState] = useState(0)

  useEffect(()=>{
    setState(5) // This is good
  },[])

  return <View />
}

Comments

Your Answer

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.