Unable to set value using useState()

18
November 24, 2020, at 7:00 PM

I'm trying to set value that is being imported from another function. But when I try to set it using useState() it returns empty array.

Code:

function something() {
    const(dataDetails,setDataDetails) = useState([])
    useEffect(()=>{
        async function fetchDataValue() {
            api call using axios => fetched data
            setMailDataList(data)
        }
        fetchDataValue()
    }, [data])
    
    return(
        <ShowValue dataValue={dataDetails} />
    )
}

another page where I'm trying to import the value:

function anotherfunction(props) {
   console.log(props.dataValue) // while doing this it prints the value
   const(newData,setNewData) = useState(props.dataValue)
   console.log(newData)   // this give me empty array 
}

So It's giving me empty array when trying to set value using useState. As I need setNewData to update the value of newData for further use so I need it in this form.

Any help will be really appreciated.

Answer 1

Can you ensure your code follow this format.

import React, { useState } from "react";
export default function App() {
  return <SomeComponent name="Hello" />;
}
const SomeComponent = (props) => {
  console.log("props.name", props.name);
  const [newState, setNewState] = useState(props.name);
  console.log("newState", newState);
  return null;
};

CodeSandBox

As you can see from codesandbox, the console is correct.

Answer 2

It should be square brackets. Instead of

const(newData,setNewData) = useState(props.dataValue)

it should be

const[newData,setNewData] = useState(props.dataValue)
READ ALSO
error when trying to run a code at pycharm and when typing pip at the cmd

error when trying to run a code at pycharm and when typing pip at the cmd

I want to start with web development with python, django, react, javascript and when I download nodejs at the windows PowerShell it showed me that its downloading python again and now when I want to run a code at PyCharm it is showing me this error:

21
Upgrading Hapi and JWT validation to latest version

Upgrading Hapi and JWT validation to latest version

I've been asked to update an old project to the latest dependencies, and I'm facing some troubles with JWT authentication

32
my get request does not work but changing it to a post works

my get request does not work but changing it to a post works

I have a get request that should return all of the logged-in user's project they created, I believe the code was written wellwhen I run it on postman I consistently get a 401 unauthorised error, but when I change the request to patch for example, and I also run a patch request...

21
Google mobile friendly url testing tool giving 502 error

Google mobile friendly url testing tool giving 502 error

I am using google search console's URL testing tools API for a while, and I am receiving a bunch of 502 bad gateway errorsI had submitted a quota increase request to bump up our default requests per day and described that we needed an increase in requests...

50