Can I add functions within the render method?

Asked

Viewed 76 times

0

It is bad practice to call functions and/or add structures (conditional, interaction) within the component render method before Return, for example:

export default class Header extends Component {

    funcao() {
        ...
    }

    render() {
        this.funcao();
        return (
            ...
        )
    }
}
  • What does this function do? I think it’s okay...

  • 3

    The way you exemplified has no validity, is not correct and why only at the time of rendering? you need to create a more real minimum example, what the function will do? maybe there are better methods to solve this and better times. Finally say what you will do, if it is generic your question can even be closed, so always focus on the problem

1 answer

0

I suggest you do it this way since you’re in classes:

export default class Header extends Component {

    componentDidMount() {

    }

    render() {
            return (
                    ...
            )
    }
}

So it will perform a function as soon as its component is rendered which I think was its goal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.