2

I am using Codesandbox to work on learning React. I am trying to conditionally render a functional React component inside of a function (inside of a class based component), that fires when a button is clicked.

Here is the link to the Codesandbox: https://codesandbox.io/embed/laughing-butterfly-mtjrq?fontsize=14&hidenavigation=1&theme=dark

The issue I have is that, without importing and rendering the Error and Meals in App.js, I never can get either component to render from the Booking component. In the function here:

   if (!this.state.name) {
     return (
       <div>
         <Error />
       </div>
     );
   }
    else {
      return <Meals name={this.state.name} date={this.state.date} />;
    }
 }

I should be rendering Error, which should then show on the screen on click if no name is inputted but nothing happens and I am stumped.

Is there anything obvious that would be preventing me from seeing the Error component from loading on the click?

Thank you in advance!

2 Answers 2

2

Everything that is displayed on the screen comes from render method. You cann't return JSX from any function like that. You can do something like this:

class Bookings extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      name: "",
      date: "",
      display: false
    };
  }

  guestInfoHandler = event => {
    console.log(this.state, "what is the state");
    this.setState({ name: event.target.value });
  };

  dateInfoHandler = event => {
    this.setState({ date: event.target.value });
  };

  showMeals = () => {
    this.setState({ display: true });
  };

  render() {
    return (
     <>
      <div style={{ display: "inline-block" }}>
        <form
          className="theForm"
          style={{
            height: "50px",
            width: "100px",
            borderColor: "black",
            borderWidth: "1px"
          }}
        >
          <label className="theLabel">
            Name:
            <input
              className="theInput"
              type="text"
              placeholder="guest name here"
              onChange={this.guestInfoHandler}
              value={this.state.value}
            />
          </label>
        </form>
        <form>
          <label>
            Date:
            <input
              type="text"
              placeholder="date here"
              onChange={this.dateInfoHandler}
              value={this.state.value}
            />
          </label>
        </form>
        <button onClick={() => this.showMeals()}>Click</button>
      </div>
      { display && name ? (
          <Meals name={name} date={name} />
          ) : (
             <Error />
          )}
     </>
    );
  }
}

export default Bookings;

Hope this works for you.

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

1 Comment

Thank you, this does work! This is so helpful and makes much more sense. I didn't think adding JSX in a function seemed right, but I found a few articles on conditional rendering that used that as an example.
0
  render() {
      const name = this.state.name;
      return (
        <div>
          {name ? (
          <Meals name={name} date={name} />
          ) : (
             <Error />
          )}
        </div>
      );
    }

nb:use render method in class component only.

there is various types conditional rendering mentioned in

https://reactjs.org/docs/conditional-rendering.html#

Comments

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.