2

I'm trying to pass a variable from a function to my React return() function. I'm not sure what the best way to do this is:

class SaveOfferModal extends Component {

  toggleSuccess() {
    const { initialValues, formValues } = this.props;
    if (initialValues !== formValues) {
      const getChangedValues = diff(initialValues, formValues);
      const showChangedValues = JSON.stringify(getChangedValues, 0, 2);
      console.log(showChangedValues);
    }

    this.setState({
      success: !this.state.success
    });
  }

render() {
return ( 
        <Button
          color='success'
          onClick={this.toggleSuccess}
          disabled={isDisabled}
          className='saveOfferBtn'
        >
          Save Offer
        </Button>
)
}

Thanks! I'm trying to pass the getChangedValues variable to the return() function!

4
  • I think you can use state or props to do that when you are using React. Do as Romans do. Commented Nov 16, 2019 at 6:13
  • I don't know what is the exact thing that you want. In react way, you have three ways to passing value to render function: state, props, and by injecting variable from the outer scope. Hope this document can help you: reactjs.org/docs/… Commented Nov 16, 2019 at 6:22
  • Your question is not clear enough. What's the variable that you need to pass to return() function Commented Nov 16, 2019 at 7:04
  • @DilanTharaka I'm trying to pass the getChangedValues variable to the return() function Commented Nov 16, 2019 at 16:35

1 Answer 1

1

You can do that using state. For example, if you have a count variable that you want to show in render

incrementCount() {
  this.setState((state) => {
    return {count: state.count + 1}
  });
}

handleSomething() {
  this.incrementCount();
  this.incrementCount();
  this.incrementCount();
}
render()
{
return ( 
        <Segment> The updated value of count is {this.state.count} </Segment>
)
}

You could also inject a variable from outside by using 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.