0

I have tried to pass array of data from parent component to child component, but in the child component I'm not getting the array of data.could you please correct me where I'm doing mistakes.

In parent component I'm getting the data from service: var tableNames = [DataService.getTableNames()]; passing the array to the child component: <DataQueryInterface tableNames={[tableNames]}/> In the child component I'm trying to destructuring like this: var tableNames = [props.tableNames]

could you please help me with this...

4
  • DataQueryInterface is the child component Commented Dec 11, 2019 at 15:08
  • 1
    Why do you call a function inside square brackets? Also, why do you put tableNames inside square brackets? Commented Dec 11, 2019 at 15:13
  • I'm calling a function inside square brackets to store the response back in a array, I'm beginner to Javascript could you please help me out Commented Dec 11, 2019 at 15:17
  • 1
    Please include a minimal reproducible example. Commented Dec 11, 2019 at 16:00

2 Answers 2

1

Here's how I would suggest to structure it:

var tableNames = [ DataService.getTableNames() ]; // Data returned by the function is the placed inside a new `[]`

If DataService.getTableNames() itself returns an array then you can directly assign it to tableNames like below:

var tableNames = DataService.getTableNames(); // no need to wrap that inside another []

Once you have that cleared, passing the array to the child component would be like below:

<DataQueryInterface tableNames={tableNames}/> 

In the child component you destructure like below:

var { tableNames } = props;
// tableNames now contains reference to passed on `[]` from parent.

Note props is an object that contains property names tableNames which contains reference to an array.

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

4 Comments

<DataQueryInterface authClicked={ authClicked } tableNames={tableNames}/> I'm passing array like this and in child component I'm destructuring like this var { tableNames } = props.tableNames; console.log(tableNames); and trying to print the data in the console from child component but I'm not getting any data in the console..
I'm getting undefined in the console
var { tableNames } = props.tableNames; is not going to work, you need to destructure from props direct.
0

If DataService.getTableNames() returns an array, you should just assign it like this var tableNames = DataService.getTableNames(); and then pass it to the component <DataQueryInterface tableNames={tableNames}/>.

Here is a simple example given what we know about the needs:

class DataService {
  getTableNames() {
    return ['John', 'Doe'];
  }
}
class Child extends React.Component {
  render() {
    const tableNames = this.props.tableNames;
    console.log('child names', tableNames)
    return (<div>{tableNames}</div>);
  }
}
class Parent extends React.Component {
  constructor() {
    super();
    const service = new DataService();
    this.tableNames = service.getTableNames();
    console.log('parent names', this.tableNames);
  }

  render() {
    return (<Child tableNames={this.tableNames}/>);
  }
}

React.render(<Parent />, document.getElementById('app'));

And here is a working jsfiddle as well https://jsfiddle.net/Lh54mfwa/

7 Comments

I have tried this, but I'm not getting the array of data in the child Component(DataInterfaceQuery).How should I pass the array of data from parent component to child component.
Did you try var tableNames = props.tableNames? You don't need the destructuring in order to get your value form the props.
You say but I'm not getting the array of data in the child Component(DataInterfaceQuery) but what are you getting in you child component using the suggested construction?
Sorry I misread your question. If your DataService returns an actual array, the suggested way to pass it down to the child should be correct.
async getTableNames(){ const response = await axios.get(`${URL}/defaultTableNames`, { headers: {'Content-Type': 'application/json'}} ) console.log("from service") console.log(response.data) return response.data } In this function calling the service and getting response back as well. var tableNames= []; const handleAuthClick = () => { setAuthClicked(true); tableNames = DataService.getTableNames(); console.log("from layout"); console.log(tableNames); }
|

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.