2

I have a problem with React and Typescript and it will be nice if I get some help from you guys!

I'm trying to assign an onclick event to my child box component but it isn't working, it doesn't trigger any error, just plainly doesn't work.

This his is the parent:

    changeActive = (issue: boolean) => {
      console.log(issue);
      this.setState({ color: true });
    }

    render() {
      return (
        <div>
            <div className="box">
              <h4 onClick={() => this.changeActive(true)}>Choose your finish.</h4>
              <div className="box--vertical">
                <Item htitle="Ivory White" onClick={() => this.changeActive} active={ this.state.color ? 'active' : ' ' } text="For the past 75 years, Sennheiser has put sound first. The new MOMENTUM True." price=" " />
                <Item htitle="Matte Black" onClick={() => this.changeActive} active={ this.state.color ? ' ' : 'active' } text="Of all of the celestial bodies that capture our attention and fascination as astronomers." price=" " />
              </div>
            </div> )};

And this is the child element:

import * as React from "react";

interface Props { active: string, htitle: string, text: string, price: string, onClick: (event: React.MouseEvent<HTMLDivElement>) => void };


export function Item(Props: Props) {
    return (
        <div className={`item ${Props.active}`} onClick={Props.onClick}>
          <div>
            <span>{Props.htitle}</span>
              {Props.text !== " " ? <p>{Props.text}</p>: ""}
          </div>
          {Props.price !== " " ? <span>+${Props.price}</span>: "" }
        </div>
      );
}

export default Item;

I'm certainly at loss here, as I'm new to react and typescript and I will like there is no error on this he execution.

5 Answers 5

1

onClick={() => this.changeActive} is wrong.

Use onClick={this.changeActive} or onClick={() => this.changeActive()}

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

1 Comment

Thank you! For some reason it didn't worked like that before, but now it's working like a charm! Thanks again!!
1

You have several issues in your code:

  1. props should not be capitalized when used as a value, only as a type. Also I'd recommend to use type Props instead of interface Props;

  2. In your onClick handler of Item component you should call the changeActive method in one of the following ways:

onClick={this.changeActive}

// or

onClick={() => this.changeActive()}
  1. It seems your render method is missing a last </div> element. It should look like this:
render() {
    return (
        <div>
            <div className="box">
              <h4 onClick={() => this.changeActive(true)}>Choose your finish.</h4>
              <div className="box--vertical">
                <Item htitle="Ivory White" onClick={() => this.changeActive()} active={ this.state.color ? 'active' : ' ' } text="For the past 75 years, Sennheiser has put sound first. The new MOMENTUM True." price=" " />
                <Item htitle="Matte Black" onClick={() => this.changeActive()} active={ this.state.color ? ' ' : 'active' } text="Of all of the celestial bodies that capture our attention and fascination as astronomers." price=" " />
              </div>
            </div>
        </div> // The missing tag
    );
}

Comments

0

You should do this. Key thing is () => this.changeActive to () => this.changeActive(true) or whatever you need.

<Item htitle="Ivory White" onClick={() => this.changeActive(true)} active={ this.state.color ? 'active' : ' ' } text="For the past 75 years, Sennheiser has put sound first. The new MOMENTUM True." price=" " />

Comments

0

It will works if you update the code as following onClick={() => this.changeActive} => onClick={() => onClick={() => this.changeActive()}

 render() {
      return (
        <div>
            <div className="box">
              <h4 onClick={() => this.changeActive(true)}>Choose your finish.</h4>
              <div className="box--vertical">
                <Item htitle="Ivory White" onClick={() => this.changeActive()} active={ this.state.color ? 'active' : ' ' } text="For the past 75 years, Sennheiser has put sound first. The new MOMENTUM True." price=" " />
                <Item htitle="Matte Black" onClick={() => this.changeActive()} active={ this.state.color ? ' ' : 'active' } text="Of all of the celestial bodies that capture our attention and fascination as astronomers." price=" " />
              </div>
            </div> )};

Comments

0

Change onClick={() => this.changeActive} to onClick={this.changeActive} in your parent component.

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.