React call component's method from nested function in render method

wklm

I would like to call the method updateCurrentThread onCLick, however I'm getting following error:

Uncaught TypeError: Cannot read property 'updateCurrentThread' of undefined

updateCurrentThread: function(thread) {
  this.setState({
  currentThread: thread
 }).bind(this);
},


render: function () {
  var threads = this.state.data.map(function (thread) {
    var boundClick = this.updateCurrentThread.bind(this, i);
    let time = getThreadDate(thread.timestamp);
    return (
      <div className="row thread" key={thread.threadID}>
        <ThreadParticipants onClick={boundClick} key={i} className="small-2 small-centered columns" ids={thread.participantIDs}/>
      </div>
  );
})
ken4z

The this inside your function is scoped to the function, not the component. If you are able to use ES6 arrow functions, which are lexically scoped, then your current approach will work. Before arrow functions, people would store the component this in a variable (e.g. var self = this) and then self.updateCurrentThread.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to render component from a function outside the render method in React?

From Dev

React render method not called for nested component

From Dev

React/Flux: Firing an action from a render method of an component or doing a Rest call from a component?

From Dev

React Native call a function in a render method

From Java

Call a React component method from outside

From Dev

Why does React call the render method of an unchanged component?

From Dev

React Native FlatList - Calling A Function In The Render Method Which Returns A Component

From Dev

Return JSX from component method to render method

From Dev

Can I call store.subscribe method of redux in render() method of react component?

From Dev

Call function from render method one time (libGDX)

From Dev

Call a 'global' function from an object's method

From Dev

react: Calling child component's method from parent component

From Dev

call instance method from higher order component in react

From Dev

call instance method from higher order component in react

From Dev

Angular 2: How to call the method on a nested component?

From Dev

Inspect a component's children recursively in render method

From Dev

Call a method from a component, in the same component

From Dev

React render nested component

From Dev

How to call a method in the React component in an external script

From Java

Should a component's render method have return type React.ReactNode or JSX.Element?

From Dev

call abstract function from a method's own implementation

From Dev

Call vuejs component's method in jquery closure

From Dev

The data-source property isn't propagated from reagent to the render method of the React Native ListView component

From Java

How to trigger the method on parent component from child using render function in Vuejs 3

From Dev

Call a method from the Vue.js component

From Dev

call a method of nested interface from enclosing class

From Dev

React - call handler from var in render function

From Dev

react call a function from ReactDOM.render

From Dev

How to call external method from nested ajax (success) method?

Related Related

  1. 1

    How to render component from a function outside the render method in React?

  2. 2

    React render method not called for nested component

  3. 3

    React/Flux: Firing an action from a render method of an component or doing a Rest call from a component?

  4. 4

    React Native call a function in a render method

  5. 5

    Call a React component method from outside

  6. 6

    Why does React call the render method of an unchanged component?

  7. 7

    React Native FlatList - Calling A Function In The Render Method Which Returns A Component

  8. 8

    Return JSX from component method to render method

  9. 9

    Can I call store.subscribe method of redux in render() method of react component?

  10. 10

    Call function from render method one time (libGDX)

  11. 11

    Call a 'global' function from an object's method

  12. 12

    react: Calling child component's method from parent component

  13. 13

    call instance method from higher order component in react

  14. 14

    call instance method from higher order component in react

  15. 15

    Angular 2: How to call the method on a nested component?

  16. 16

    Inspect a component's children recursively in render method

  17. 17

    Call a method from a component, in the same component

  18. 18

    React render nested component

  19. 19

    How to call a method in the React component in an external script

  20. 20

    Should a component's render method have return type React.ReactNode or JSX.Element?

  21. 21

    call abstract function from a method's own implementation

  22. 22

    Call vuejs component's method in jquery closure

  23. 23

    The data-source property isn't propagated from reagent to the render method of the React Native ListView component

  24. 24

    How to trigger the method on parent component from child using render function in Vuejs 3

  25. 25

    Call a method from the Vue.js component

  26. 26

    call a method of nested interface from enclosing class

  27. 27

    React - call handler from var in render function

  28. 28

    react call a function from ReactDOM.render

  29. 29

    How to call external method from nested ajax (success) method?

HotTag

Archive