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 = "[email protected]", user2 = "[email protected]".

-----------------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={"[email protected]"}
            user2={"[email protected]"}
          />
        );
        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 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
ADVERTISEMENT