received this error "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop."

Melissa

Im new in reactjs I am receiving this error: "Error: Too many re-renders. React limits the number of renders to prevent an infinite loop." and I am not sure how to solved it.

const MAPBOX_TOKEN = ''; ///add token here

export default function MapHooks(){
    const [mapping, setMapping] = useState({
        width: 800,
        height: 600,
        longitude: -122.45,
        latitude: 37.78,
        zoom: 14
      });

    function _onViewportChange(mapping){
        setMapping({mapping})
    }

    function _goToNYC() {
        const viewport = {
            ...mapping,
            longitude: 101.7412,
            latitude: 3.1549,
            zoom: 14,
            transitionDuration: 5000,
            transitionInterpolator: new FlyToInterpolator(),
            transitionEasing: d3.easeCubic
        };
        setMapping({viewport}) 

    }

    return (
        <div>
            <button onClick={_goToNYC()}>New York City</button>
            <MapGL
            {...mapping}
            mapStyle="mapbox://styles/mapbox/dark-v10"
            onViewportChange={_onViewportChange()}
            mapboxApiAccessToken={MAPBOX_TOKEN}
            ></MapGL>
        </div>
    );
}

Please help, thank you :)

Jayce444

You shouldn't be executing the function when assigning it to the onClick, you should just be passing it in like so:

<button onClick={_goToNYC}>New York City</button>

If you actually call it when you assign it, then the function will execute. In that execution you call setMapping(...), which will trigger a re-render. Which will then cause that function to be executed again when the button renders, and so on -> infinite loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SFTP error "Received message too long"

Unexpected system error after push was received

BSXPCMessage received error for message: Connection interrupted

why I received the error "uninitialized constant SecureRandom?

cloudkit error no authToken received for asset

Error: [filter:notarray] Expected array but received: 0

Error received Bad JSON escape sequence

Post received by FromBody causes serializable error

Error Received while building the Auto encoder

Error parsing parameter '--parameters': Expected: '=', received: 'P'

When i add this code in workbook received an error

strongswan: received NO_PROPOSAL_CHOSEN notify error

Error received from database was #1064 - PHP & MySQL

Replacing Values in R - Error Received

Why I received an Error 403 with MockMvc and JUnit?

Inactive status error received - JQuery

Received error when processing identity validation filter

CreateProcessWithLogonW Error 1783: The stub received bad data

too long string error received Error 414

sftp Received disconnect 11: Application error

oracle PL/SQL error received

Error: [orderBy:notarray] Expected array but received: {}

expect(received).toEqual(expected) - error

JS Jest Mocking - expected received undefined error

HTTP response is being received as error in angular 7

Channel:mychannel received discovery error:access denied

Angular: Error received at the time of capturing field

Error while reading image received from socket

Formik tRPC error "Expected string, received null"

'Error: expect(received).toContain(expected) // indexOf' error