0

I have a class like the one below, I added a new parameter in mapStateToProps, and I can see it when rendering. I don't know how to pass it to the onClick method. Any Idea? I prefer not to pass it directly when I call the function, there are other parameters already accessible that are not passed directly.

class A extends Component{
  static propTypes = {
    myValue: PropTypes.string.isRequired
  }

  onClick = (param1, param2) => {
    >>>I want to access myValue here<<<
  }

  render() {
    const { myValue } = this.props
      return (
        <MyCompoment
          onClick={this.onClick}
        >
      )
  }
}

const mapStateToProps = (state, props) => ({
  myValues: getMyValue(state),
})
4
  • 1
    isn't myValues accessible to you via this.props.myValues? Commented Jun 16, 2020 at 12:02
  • it is actually, I though there was another way because I already have some parameters that are available without accessing props... Commented Jun 16, 2020 at 12:04
  • onClick = (param1, param2) => { Commented Jun 16, 2020 at 12:08
  • an event handler is passed a synthetic Event object by react. So that will be one of the parameters. What is second paramter? Commented Jun 16, 2020 at 12:10

1 Answer 1

1

You can access it like:

onClick = (param1, param2) => {
    const test = this.props.myValues;
}

or like render method:

onClick = (param1, param2) => {
    const { myValue } = this.props;
}
Sign up to request clarification or add additional context in comments.

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.