Question

This is involving javascript using react. The problem is to pass 2 variables of data from...

This is involving javascript using react. The problem is to pass 2 variables of data from a parent component to a child component. Pass these two variables to the child component: user1 = "admin@email.org", user2 = "cust@email.org".

-----------------Parent.js-----------------

import React from "react";
import ReactDOM from "react-dom";
import GuestApp from "./GuestApp";

class App extends React.Component {

constructor(props) {

super(props); // Must call

this.state = {role: "admin"};

}

render() {

switch (this.state.role) {

case 'guest':

return <GuestApp /* Insert the passing data in here */ />;

break;
}
}
}
ReactDOM.render(<App />, document.getElementById("root"));

-----------------Child.js-----------------
import React from "react";

class GuestApp extends React.Component

{

constructor(props)

{

super(props); // Must call

this.state = {show: "home"};

}
render()
{
/* Insert any code here as needed to accept the passed down information from parent.js */
}
}

Modify the parent and child files as needed to pass down the information from parent to child. Need not worry about any component rendering or anything else. This exercise is only to correctly pass information down.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can do this in multiple ways but here I have passed it down as a prop and the used it using object destructuring. My code is below.

This is the parent component

import React from "react";
import ReactDOM from "react-dom";
import GuestApp from "./GuestApp";
class App extends React.Component {
  constructor(props) {
    super(props); // Must call
    this.state = { role: "admin" };
  }
  render() {
    switch (this.state.role) {
      case "guest":
        return (
          <GuestApp
            /* Insert the passing data in here */
            user1={"admin@email.org"}
            user2={"cust@email.org"}
          />
        );
        break;
    }
  }
}
ReactDOM.render(<App />, document.getElementById("root"));

This is the child component.

import React from "react";
class GuestApp extends React.Component {
  constructor(props) {
    super(props); // Must call
    this.state = { show: "home" };
  }
  render() {
    /* Insert any code here as needed to accept the passed down information from parent.js */
    const { user1, user2 } = this.props;
  }
}

I hope you liked it.

Add a comment
Know the answer?
Add Answer to:
This is involving javascript using react. The problem is to pass 2 variables of data from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This question is based on react js. Please don't do it if you don't have thorough...

    This question is based on react js. Please don't do it if you don't have thorough knowledge of React,node,mongodb, express.angular and vue.js. So i am designing an engineering dashboard with live api tracking system with frontend in react and backend in node js. You need to design a fully functional dashboard you can take any dummy database make apis test them and then add pagination in it. Please don't attempt it if you don;t have knowledge of rest api,react and...

  • This week we want to build an inheritance hierarchy using targets. You now need to take...

    This week we want to build an inheritance hierarchy using targets. You now need to take the Target class we have built and create a subclass that inherits from it. You can do whatever you want here, but it should be obvious how you changed the target. Some examples might include adding an additional ellipse to the target to increase the number of levels or changing up the colors. Once you have come up with one way to change the...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT