score:7
Accepted answer
issue is with this line:
{this.props.postcomments.map( this.rendercomment )}
because you forgot to bind rendercomment
, map callback method, so this
inside rendercomment
method will not refer to the class context.
use any one of these solutions, it will work.
1- use this line in constructor
:
this.rendercomment = this.rendercomment.bind(this) ;
2- pass this
with with map
like:
{this.props.postcomments.map(this.rendercomment, this)}
3- use arrow function with rendercomment
method, like this:
rendercomment = (comment, i) => {
.....
or use the map inside the rendercomment
function (i used to prefer this way), like this:
rendercomment() {
return this.props.postcomments.map((comment, i) => {
return(
<div classname="comment" key={i}>
<p>
<strong>{comment.user}</strong>
{comment.text}
<button
onclick={this.handleremovecomment}
classname="remove-comment">
×
</button>
</p>
</div>
)
})
}
and call this method from render
, in this case binding of rendercomment
is not required:
{this.rendercomment()}
Source: stackoverflow.com
Related Query
- How to access correct 'this' inside map: ReactJS
- Reactjs how to use ref inside map function?
- How to map inside a map function in reactjs
- Cannot access correct this inside an axios callback
- How to use this highchart map in reactJS
- reactjs how to call method inside map function
- How to run map inside map function in Reactjs
- How to map inside map function and show the data in Reactjs
- How can i access the history object inside of this promise call?
- How to access variable values inside componentWillMount() in reactjs
- How can I access a value of a variable inside a map function?
- How to set individual states to render components inside a map function in ReactJs
- ReactJS - How to get access to state value inside a funciton?
- How to access Nested Object with Map in ReactJS
- How to access variables inside render in ReactJS from another class (EDIT: another js page) (ES6 and Node is being used)
- How sort a value inside a map looping in ReactJS
- how to map through a property inside an array of arrays of objects - reactjs
- How to map this api response and display in HTML - ReactJS - NextJS
- How to display google map in reactjs inside of page height and width?
- How to access next/prev object inside map - react?
- How do I display base64 inside map loop(assumption) in ReactJS
- How can I access the data inside this function? I believe it is a promise, but I'm not sure
- How to get DOM element id with OnClick inside map fuction in ReactJs
- reactjs this context is changed inside on change method how call function
- How to access state inside function which is inside of function inside of map
- How can I correct this error in ReactJS / NodeJS / MySQL?
- How map throw forEach() inside the filter() in ReactJs
- ReactJS - Use this inside a Listener from Google Map
- How I can access to values inside of this object returned from an API call
- How to display objects that are inside array using map method in Reactjs
More Query from same tag
- how to get list of all registered synthetic event handlers/listeners for a React Component?
- How to disable ripple effect on primary action in react material lists
- React hooks useState to set multiple states in context provider
- State using React Typescript: Property does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes
- React bind to HTML element outside of reactDOM
- How to setState of a specific slider?
- Change the clock icon color in MUI TextField with type 'time'
- Space between two buttons in Material Design
- How use $PropertyType for function stateless components of react?
- How do I pass array items in an component and return it inside of another component
- ReactJS setState with object and array
- Hidden Input/Textarea value onChange setState in React - without user typing inputs
- REACTJS - Import CKEditor 5 from online build
- Error in react-toastify/dist/ReactToastify.css Module parse failed .Toastify__toast-container { | React SSR
- Linking functions with Promises in React
- I don't understand why this is happening when both code are aparently same
- React - Returning data from API
- Regex Validation for userInput in React
- Uncaught SyntaxError: Unexpected token '<' React
- React changing state in Parent , does render calls for all its children and sub-children as iteration?
- Background Image Style is not working in react
- React: Fix missing dependency warning useEffect with Refs
- CORS error while authorize application to call API Azure AD?
- Pass props to a component in a Route at runtime
- How do I render a component when the data is ready?
- How to create dynamic form input fields in React with ANTd
- Expand state array length based on user input
- Send email from React application
- Bind a function in parent to a mouse event in child in react (hooks)
- ant design Layout issue with position fixed Sider