Answer the question
In order to leave comments, you need to log in
Why does React not see props from Redax in the handler when it sees it in render(){}?
There is this component:
import React, { Component } from "react";
import NewStory from "../NewStory";
import "./index.scss";
import MaterialIcon from "material-icons-react";
import history from "../../history";
import { connect } from "react-redux";
class Dashboard extends Component {
state = {
addNewOpen: false
};
handleCreateNew = () => {
this.setState({
addNewOpen: !this.state.addNewOpen
});
};
clickHandle(e) {
e.preventDefault();
let targetElement = e.currentTarget.dataset.id;
const { stories } = this.props; // ТУТ НЕ ВИДИТ
if (stories) {
Object.keys(stories).map(element => {
if (targetElement === element) {
history.push({
pathname: "/story",
state: { detail: element }
});
}
});
}
}
render() {
const { addNewOpen } = this.state;
const { stories } = this.props; // ТУТ ВИДИТ
const dashClass = stories ? "dashboard not-empty" : "dashboard empty";
let elements = stories ? (
Object.keys(stories).map(key => {
let imgUrl = stories[key].img ? stories[key].img : "img/no_image.jpg";
return (
<a
key={key}
href="/"
onClick={this.clickHandle}
data-id={key}
className="stories__element">
<img className="stories__element-image" src={imgUrl} alt="image" />
<div className="overlay" />
<h2 className="stories__element-title">{stories[key].title}</h2>
<div className="stories__element-labels">{stories[key].labels}</div>
</a>
);
})
) : (
<div>Loading...</div>
);
const content = stories ? (
<div className="stories">
<div
className="create-new stories__element"
onClick={this.handleCreateNew}>
<div className="overlay" />
<h2 className="create-new__title">
<MaterialIcon icon="add" size={48} color="#000" />
Add new
</h2>
</div>
{elements}
</div>
) : (
<div>
<div className="create-new" onClick={this.handleCreateNew}>
<MaterialIcon icon="alarm_add" color="#68edc6" size={100} />
<h2 className="create-new__title">Add new</h2>
</div>
</div>
);
return (
<div className={dashClass}>
<NewStory isOpen={addNewOpen} />
{content}
</div>
);
}
}
const mapDispatchToProps = {};
export default connect(
state => ({
stories: state.stories
}),
mapDispatchToProps
)(Dashboard);
main.js:92935 Uncaught TypeError: Cannot read property 'props' of undefined
Answer the question
In order to leave comments, you need to log in
You are passing the clickHandle method to the click event listener callback , when called, the method loses its context because it is not called on your object. this in this case does not refer to your object, but to undefined . Since babel adds 'use strict' when translating (otherwise, when executed in a browser, it would refer to window ). There are several ways to fix this:
1. remake the handler from a class method into a class field arrow function :
was:
clickHandle(e) {
// some code
}
clickHandle = e => {
// some code
};
constructor(props) {
super(props);
this.clickHandle = this.clickHandle.bind(this);
}
<ListItem
key={item.id}
onClick={e => this.сlickHandle(item.id, e)}
>
{item.name}
</ListItem>
var john = {
firstName: 'John',
getName() {
return this.firstName; // this - контекст вызова и это не всегда наш объект
}
}
var foo = {
firstName: 'foo',
};
var foo.getName = john.getName;
console.log(foo.getName()); // foo
var bar = john.getName;
console.log(bar()); // undefined
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question