score:2
The solution was here:
class PickColor extends React.Component {
getColorValue(event) {
console.log(event.target.getAttribute("data-color"));
this.props.color(event.target.getAttribute("data-color"));
}
render () {
var colors = ['red', 'purple', 'yellow', 'green', 'blue'],
colorsLink = [],
that = this;
colors.forEach(function(el){
colorsLink.push(<li data-color={el} key={el} onClick={that.getColorValue.bind(that)} ref={el}>{el}</li>);
});
return (
<ul>
{colorsLink}
</ul>
);
}
}
I had to bind "that" to the onClick inside the forEach loop
score:0
When you create a new function, like this:
function() {
this.props.action(name);
});
This is bind to the new function context. Every function has a different this
in javascript. You can solve this in a few ways:
Use arrow functions if you have them (they won't rebind this)
() => {
this.props.action(name);
});
Rebind this with bind
function() {
this.props.action(name);
}.bind(this));
Save this
in a variable
var that = this;
function() {
that.props.action(name);
});
Choose the first if you have a transpiler, like babel! Otherwise it's your call.
score:1
Issue is in this line:
onClick={that.getColorValue}
You forgot to bind the correct this (class context) with onClick event handler function because of that this.props
is not accessible in getColorValue
function.
Solutions:
Multiple Solutions are possible, use any one (all these changes are for PickColor component):
1- Inline binding of click handler:
onClick = { that.getColorValue.bind(this) }
2- Bind the method in the constructor:
constructor(props) {
super(props);
this.state = { };
this.getColorValue = this.getColorValue.bind(this);
}
3- Using arrow function:
onClick = {e => that.getColorValue(e) }
4- Using class property syntax:
onclick = {this.getColorValue}
getColorValue = (e) => {
console.log('e', this.props)
}
Source: stackoverflow.com
Related Query
- React JS props not working .. Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- React props - set isRequired on a prop if another prop is null / empty
- React Testing Library: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- Why is there a semi-colon in the object structure for expected props in this typescript react component?
- React default props value using null
- React check null for props function call
- React Component Array Map - Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- React - Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- React Redux: Why is this nested component not receiving props from redux state?
- Why is this react handleChange not working? Trying to handle a change received by props
- Props becoming null after they are passed into react component
- How to pass props or state from parent to child react in this format
- React js, pass a function with parameters as props without using this keyword
- On page reload props in null when using react router
- this is null in React class function
- What causes the failure to pass props from parent to child in this React application?
- React Error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- TypeScript, React causing errors using forwardRef with props & ref is null
- How to use either this type or another based on props only? React Typescript Discriminating Unions
- How is this react controlled component input's value updated when no props are passed
- How to make this component looped in React (by counting the fat thact it receives props and does some calculation and has static titles)
- How to pass React component as a child and add props inside this conponent
- How can a function correctly accept a React component and its props as arguments in TypeScript and render this component?
- React - Handling Null values passed as props
- Where can I find the API guide for this usage of passing props to children in React
- How do i change this react router v5 code that uses props to v6
- REACT with ts - checking props is null in tsx.view
- Why user data is null in props in React Js?
- React redux mapped state to props array is null on update
- Access props inside quotes in React JSX
More Query from same tag
- Using the proxy setting for React to get data from a Featherjs backend
- ReactJS file-loader not working
- Getting this.startBreak() is not a function error in react.js
- Passing Object and its Methods to another function
- How to declare props for react-select in typescript?
- React Redux - Mapping over array while rerendering only components holding changed object (in array)
- InputProps min and max not being enforced
- TypeError: react__WEBPACK_IMPORTED_MODULE_0___default is not a function or its return value is not iterable
- amchart bullet change background color by mouse hover
- KendoReact Chart panning issue for ChartSeriesItem with map function
- Get Data using Redux with react hooks
- Why do ReactDOMServer and ReactDOM give the "root.hydrate is not a function" error
- How can i Display product item 1 by 1 by clicking a product ? i already call the id of the product but i cant display the whole data of product
- React testing library - TypeError: expect(...).toHaveTextContent is not a function
- Why is my component not rendering?
- useEffect - Can't perform a React state update on an unmounted component
- Make a Material UI Component in React Sticky when scrolling (not AppBar)
- Confusing Syntax of Spreading React Props
- React complains that hook is not used inside of a body function when my component is passed into another component to render
- Jest - Mock Nested Function
- big size of svg not working proper how to set big svg in react-pan-zoon-svg?
- ref prop on textarea; "No overload matches this call'
- ESLint error: 'default' is restricted from being used as an exported name
- React Router 4, how to check for a possible redirect on all routes?
- How to test Parent passing `this` to Children using enzyme
- React: is componentDidUpdate same for 2 different instances of a component?
- React custom mount hook
- I am trying to change the content of a an html element rendered by React.js using onClick. But this code doesn't seem to work
- How to use Ellipsis in react-bootstrap pagination
- "this" is not recognizing in react's input for getting value