React Js onClick inside render

Khawer Zeshan

I have a ul li list inside the render method and there is an onClick event on li which call this.handleClick function and state gets changed

var InspirationFilter = React.createClass({
    getInitialState: function() {
        return {currentFilterText:"Top All Time", currentFilter:"top_all_time", showUl:false};
    },
    handleClick: function(filter, filterText){
        this.setState({currentFilterText:filterText, currentFilter:filter, showUl:!this.state.showUl});
    },
    render: function() {
        return (
            <ul>
                <li onClick={this.handleClick('top_all_time', 'Top All Time')}>Top All Time</li>
                  <li onClick={this.handleClick('top_this_week', 'Top This Week')}>Top Week</li>
                <li onClick={this.handleClick('top_this_month', 'Top This Month')}>Top Month</li>
            </ul>
        );
    }
});

But this code gives me the error

Warning: setState(...): Cannot update during an existing state transition (such as within render). Render methods should be a pure function of props and state

I tried to use bind with the click event like this,

return (
    <ul>
        <li onClick={this.handleClick.bind(this,'top_all_time', 'Top All Time')}>Top All Time</li>
        <li onClick={this.handleClick.bind(this,'top_this_week', 'Top This Week')}>Top  Week</li>
        <li onClick={this.handleClick.bind(this.'top_this_month', 'Top This Month')}>Top  Month</li>
    </ul>
);

The above error is gone now but ran into another error which is as follows,

Warning: bind(): You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call. See InspirationFilter

In the react documentation Communicate Between Components they are also using the bind method.

Any suggestions to fix it?

Rene Saarsoo

The problem is that onClick value has to be a function, so just create a helper method that creates such a function for you:

createClickHandler: function(filter, filterText){
    return function() {
        this.setState({...});
    }.bind(this);
},

render: function() {
    return (
        <ul>
            <li onClick={this.createClickHandler('top_all_time', 'Top All Time')}>Top All Time</li>
            <li onClick={this.createClickHandler('top_this_week', 'Top This Week')}>Top Week</li>
            <li onClick={this.createClickHandler('top_this_month', 'Top This Month')}>Top Month</li>
        </ul>
    );
}

React is really just a bit over-helpful here with its warning. Your reason for using bind() is no to bind this, but rather to bind the arguments.

Why it works? This solution avoids the re-binding warning because it's not re-binding an already bound this.handleClick, but instead creating a new function (that's not yet bound to anything) and binding that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Render new element onClick in react.js

From Java

Why is my onClick being called on render? - React.js

From Dev

Is there a way to conditionally render content using onClick in react JS?

From Dev

React ternary inside of the render

From Dev

React.js How to render component inside component?

From Dev

Render components inside other components with react.js

From Java

React onClick function fires on render

From Dev

React -render values onClick of tabs

From Dev

React, Adding onClick event directly to Components instead of render element inside said component.

From Dev

React JS isomorphic render

From Dev

onClick not working React js

From Dev

React.js return a style property from a function and use it inside the render() function

From Dev

Return multiple elements inside React.render()

From Java

How to Call a Function inside a Render in React/Jsx

From Java

Rendering React components with promises inside the render method

From Dev

Using jQuery inside of a React render function

From Dev

react: Show javascript inside render as plain text

From Dev

React render component inside a JSX argument

From Dev

Dynamically render a component inside a another component in React

From Dev

react.js - Render on requestAnimationFrame

From Dev

Render staggering images in React JS

From Dev

Render dynamic html in react js

From Dev

Image - onclick event in React JS

From Dev

React JS - React Router - Render content separately

From Dev

onClick doesn't render new react component.

From Dev

React.js: Refs are not available on initial render

From Dev

React js - Disable render of a component in a mixin

From Dev

Render React JS server side using .net

From Dev

Loop and render an object in React.js

Related Related

  1. 1

    Render new element onClick in react.js

  2. 2

    Why is my onClick being called on render? - React.js

  3. 3

    Is there a way to conditionally render content using onClick in react JS?

  4. 4

    React ternary inside of the render

  5. 5

    React.js How to render component inside component?

  6. 6

    Render components inside other components with react.js

  7. 7

    React onClick function fires on render

  8. 8

    React -render values onClick of tabs

  9. 9

    React, Adding onClick event directly to Components instead of render element inside said component.

  10. 10

    React JS isomorphic render

  11. 11

    onClick not working React js

  12. 12

    React.js return a style property from a function and use it inside the render() function

  13. 13

    Return multiple elements inside React.render()

  14. 14

    How to Call a Function inside a Render in React/Jsx

  15. 15

    Rendering React components with promises inside the render method

  16. 16

    Using jQuery inside of a React render function

  17. 17

    react: Show javascript inside render as plain text

  18. 18

    React render component inside a JSX argument

  19. 19

    Dynamically render a component inside a another component in React

  20. 20

    react.js - Render on requestAnimationFrame

  21. 21

    Render staggering images in React JS

  22. 22

    Render dynamic html in react js

  23. 23

    Image - onclick event in React JS

  24. 24

    React JS - React Router - Render content separately

  25. 25

    onClick doesn't render new react component.

  26. 26

    React.js: Refs are not available on initial render

  27. 27

    React js - Disable render of a component in a mixin

  28. 28

    Render React JS server side using .net

  29. 29

    Loop and render an object in React.js

HotTag

Archive