score:0
the function passed to this.props.companies.map
isn’t an arrow function, so it creates a new this
. change it to an arrow function to preserve the this
from outside of it.
this.props.companies.map( ( item, i ) => { ... } )
you’ve also named the argument to onclick
item
, but it’s actually the click event. you want the item
already defined by the map
function. name the argument to onclick
something else, or nothing, to avoid overwriting the item
variable you actually want.
onclick={ () => { ... } }
score:2
make sure the function given to map
is bound as well, or an arrow function:
{this.props.companies.map((item, i) => {
return (
<div>
<div
key={i}
onclick={() => {
this.setstate({ currentcompany: item });
}}
>
{i}: {item.name}
</div>
<button>delete company</button>
</div>
);
})}
Source: stackoverflow.com
Related Query
- How to change State from inside .Map function React
- React: How to add onChange functionality inside of Function component using React Hooks? Need onClick event from a checkbox to influence input state
- How do I change the component from inside a map function in react?
- How to change a object state inside a map function
- How to access state from another function inside React useEffect
- How can I dispatch a state change to redux when calling it from a function inside the component?
- how to make state work in only one div inside map function in react
- How to update state of child component when it get called in map function from parent component React JS
- how and when to call a react component methods after state change from redux
- How to loop inside map function using jsx format React JS
- React - How do you call a function from inside another function
- React Native: How to stop map markers from re-rendering on every state update
- How to wait for a Redux action to change state from a React component
- How do I pass a React Context's state through an onClick function created in a method inside that context class?
- How to access from state another key inside the state in React
- React how to access state from a function in another file?
- How to use Bootstrap Modal inside map function in React Js to render indivisual elements?
- How to map an array of strings from react state to a select box
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to use map function on react state hook
- React How to change state value from child to Parent using useState and useContext
- React hooks not returning updated state values inside callback function from library events (FabricJS)
- React : how to pass and array from inside a Function to the return (JSX)
- How to Use State inside the function in React JS?
- How do you change state of a react component from a different component?
- how do i update state in react for updating the value of an object inside an array using array.map function
- How do I change react state using a promise with Formik inside handleSubmit to show/hide a div?
- React - How to condition the execution of a function on the state (on the change of state)?
- How to change the state of one class from a function in a different class
- How can I change state of current map item in react component
More Query from same tag
- React-Router will not render components
- Radio Button Selection in React Using Formik
- ShallowWrapper selector in enzyme
- React setState(null) and force update
- How to avoid repetitive code in this scenario?
- Where to set up react router tags?
- Fetch and setInterval react hooks problem
- React: State is not up-to-date when I unmount component
- List Item link using Material UI
- in Typescript, How to ensure React child components are of a certain type
- Normalizr - How to handle nested entities that are already normalized
- How to search based on multiple criteria in AutoComplete API in material UI
- How to compare current user id with the id from one collection in firebase using React
- Confused about connect function of Redux
- Objects are not valid as a React child (...) If you meant to render a collection of children, use an array instead
- Props not being used correctly in styled-components div's CSS
- How can i get the context of the MUI theme?
- react-pose: enter from left, exit from right
- How to map all results from Twitch API using cursor - React
- How to mock class instance exported from file with another class instance
- Incrementing score in react quiz app (multivariable score)
- Reactjs counter onClick
- How to update input value using ref in ReactJS
- How to clear a settimeout in react
- React Router not rendering component initially
- Next.js + React Go back to the previous page
- Props are not passing from parent to child components in React Functional component
- ReactJS apolloClient Could not find "client" in the context or passed in as an option
- Flow Type aliases on React stateless components
- Components with large datasets runs slow on IE11/Edge only