Getting “Uncaught TypeError” in a React app
I am new to React and I'm trying to make a "delete button" on my Twitter-ish app for my practice. It should look just like the real Twitter, there's a list of all of my tweets and each tweet has a delete button.
I'm successfully showing all of my tweets, but I'm currently getting an error around my delete button saying Uncaught TypeError: this.props.onDelete is not a function
and I have no idea how to fix it. How can I make it to work?
My codes are as follows:
Tweet.js
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Tweets.js
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} />;
});
return (
<div>
{tweets}
</div>
);
}
}
export default Tweets;
TweetsPage.js
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
const url = '/tweets/index.json';
$.ajax({
url: url,
dataType: 'json',
type: 'GET',
cache: false,
success: (data) => {
this.setState({
tweets: data
});
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
onSubmitDelete(id) {
const url = '/tweets/destroy';
$.ajax({
url: url,
type: 'POST',
cache: false,
data: {
id: id
},
success: (data) => {
this.loadTweetsFromServer();
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
javascript reactjs
add a comment |
I am new to React and I'm trying to make a "delete button" on my Twitter-ish app for my practice. It should look just like the real Twitter, there's a list of all of my tweets and each tweet has a delete button.
I'm successfully showing all of my tweets, but I'm currently getting an error around my delete button saying Uncaught TypeError: this.props.onDelete is not a function
and I have no idea how to fix it. How can I make it to work?
My codes are as follows:
Tweet.js
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Tweets.js
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} />;
});
return (
<div>
{tweets}
</div>
);
}
}
export default Tweets;
TweetsPage.js
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
const url = '/tweets/index.json';
$.ajax({
url: url,
dataType: 'json',
type: 'GET',
cache: false,
success: (data) => {
this.setState({
tweets: data
});
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
onSubmitDelete(id) {
const url = '/tweets/destroy';
$.ajax({
url: url,
type: 'POST',
cache: false,
data: {
id: id
},
success: (data) => {
this.loadTweetsFromServer();
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
javascript reactjs
add a comment |
I am new to React and I'm trying to make a "delete button" on my Twitter-ish app for my practice. It should look just like the real Twitter, there's a list of all of my tweets and each tweet has a delete button.
I'm successfully showing all of my tweets, but I'm currently getting an error around my delete button saying Uncaught TypeError: this.props.onDelete is not a function
and I have no idea how to fix it. How can I make it to work?
My codes are as follows:
Tweet.js
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Tweets.js
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} />;
});
return (
<div>
{tweets}
</div>
);
}
}
export default Tweets;
TweetsPage.js
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
const url = '/tweets/index.json';
$.ajax({
url: url,
dataType: 'json',
type: 'GET',
cache: false,
success: (data) => {
this.setState({
tweets: data
});
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
onSubmitDelete(id) {
const url = '/tweets/destroy';
$.ajax({
url: url,
type: 'POST',
cache: false,
data: {
id: id
},
success: (data) => {
this.loadTweetsFromServer();
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
javascript reactjs
I am new to React and I'm trying to make a "delete button" on my Twitter-ish app for my practice. It should look just like the real Twitter, there's a list of all of my tweets and each tweet has a delete button.
I'm successfully showing all of my tweets, but I'm currently getting an error around my delete button saying Uncaught TypeError: this.props.onDelete is not a function
and I have no idea how to fix it. How can I make it to work?
My codes are as follows:
Tweet.js
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Tweets.js
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} />;
});
return (
<div>
{tweets}
</div>
);
}
}
export default Tweets;
TweetsPage.js
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
const url = '/tweets/index.json';
$.ajax({
url: url,
dataType: 'json',
type: 'GET',
cache: false,
success: (data) => {
this.setState({
tweets: data
});
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
onSubmitDelete(id) {
const url = '/tweets/destroy';
$.ajax({
url: url,
type: 'POST',
cache: false,
data: {
id: id
},
success: (data) => {
this.loadTweetsFromServer();
},
error: (xhr, status, err) => {
console.error(url, status, err.toString());
},
});
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
javascript reactjs
javascript reactjs
asked Nov 21 at 1:50
ta539tg70
398
398
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You need to pass the handler to your child. Rest of your code is perfect.
TweetsPage.js - No change in this file
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
// load and set state
}
onSubmitDelete(id) {
// submit delete req to server
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
Tweets.js - Note the changes
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
let { onDelete, tweets } = this.props; // note this line
// note the next line - arrow function and different variable name
const renderedTweets = tweets.map((tweet) => {
// note the next line
return <Tweet tweet={tweet} key={tweet.id} onDelete={onDelete} />;
});
return (
<div>
{renderedTweets}
</div>
);
}
}
export default Tweets;
Tweet.js - No change in this file
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
add a comment |
You pass the onDelete
function as a prop to your Tweets component but not to your Tweet component. Your Tweet component tries to call it in the handleDelete function. To fix this, pass the prop onto the Tweet component.
render() {
let that = this
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} onDelete={that.props.onDelete} />;
});
return (
<div>
{tweets}
</div>
);
}
Thank you for your reply. I just modified my code just like the way you showed and I'm now gettingUncaught TypeError: Cannot read property 'props' of undefined
...
– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors inonSubmitDelete
ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404236%2fgetting-uncaught-typeerror-in-a-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to pass the handler to your child. Rest of your code is perfect.
TweetsPage.js - No change in this file
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
// load and set state
}
onSubmitDelete(id) {
// submit delete req to server
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
Tweets.js - Note the changes
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
let { onDelete, tweets } = this.props; // note this line
// note the next line - arrow function and different variable name
const renderedTweets = tweets.map((tweet) => {
// note the next line
return <Tweet tweet={tweet} key={tweet.id} onDelete={onDelete} />;
});
return (
<div>
{renderedTweets}
</div>
);
}
}
export default Tweets;
Tweet.js - No change in this file
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
add a comment |
You need to pass the handler to your child. Rest of your code is perfect.
TweetsPage.js - No change in this file
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
// load and set state
}
onSubmitDelete(id) {
// submit delete req to server
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
Tweets.js - Note the changes
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
let { onDelete, tweets } = this.props; // note this line
// note the next line - arrow function and different variable name
const renderedTweets = tweets.map((tweet) => {
// note the next line
return <Tweet tweet={tweet} key={tweet.id} onDelete={onDelete} />;
});
return (
<div>
{renderedTweets}
</div>
);
}
}
export default Tweets;
Tweet.js - No change in this file
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
add a comment |
You need to pass the handler to your child. Rest of your code is perfect.
TweetsPage.js - No change in this file
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
// load and set state
}
onSubmitDelete(id) {
// submit delete req to server
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
Tweets.js - Note the changes
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
let { onDelete, tweets } = this.props; // note this line
// note the next line - arrow function and different variable name
const renderedTweets = tweets.map((tweet) => {
// note the next line
return <Tweet tweet={tweet} key={tweet.id} onDelete={onDelete} />;
});
return (
<div>
{renderedTweets}
</div>
);
}
}
export default Tweets;
Tweet.js - No change in this file
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
You need to pass the handler to your child. Rest of your code is perfect.
TweetsPage.js - No change in this file
import React from 'react'
import Tweets from './Tweets'
class TweetsPage extends React.Component {
constructor() {
super();
this.state = {
tweets: ,
id: '',
};
this.onSubmitDelete = this.onSubmitDelete.bind(this);
}
loadTweetsFromServer() {
// load and set state
}
onSubmitDelete(id) {
// submit delete req to server
}
componentDidMount() {
this.loadTweetsFromServer();
}
render() {
return (
<div>
<Tweets tweets={this.state.tweets} onDelete={this.onSubmitDelete} />
</div>
);
}
}
export default TweetsPage;
Tweets.js - Note the changes
import React from 'react'
import Tweet from './Tweet'
class Tweets extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
}
render() {
let { onDelete, tweets } = this.props; // note this line
// note the next line - arrow function and different variable name
const renderedTweets = tweets.map((tweet) => {
// note the next line
return <Tweet tweet={tweet} key={tweet.id} onDelete={onDelete} />;
});
return (
<div>
{renderedTweets}
</div>
);
}
}
export default Tweets;
Tweet.js - No change in this file
import React from 'react'
class Tweet extends React.Component {
constructor(props) {
super(props);
this.state = {
tweet: this.props.tweet,
id: this.props.id,
};
this.handleDelete = this.handleDelete.bind(this);
}
handleDelete(id) {
this.props.onDelete(id);
}
render() {
const tweet = this.state.tweet;
return (
<div>
<p>{tweet.user.user_name} {tweet.created_at}</p>
<p>{tweet.tweet}</p>
<button onClick={this.handleDelete(tweet.id)}>DELETE</button>
</div>
);
}
}
export default Tweet;
answered Nov 21 at 2:39
Dinesh Pandiyan
2,427825
2,427825
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
add a comment |
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
Your suggestion worked perfectly as well. It looks like I now have errors in onSubmitDelete ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:47
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
If this solved your problem, can you mark the answer as correct answer? It will help others with similar problem.
– Dinesh Pandiyan
Nov 21 at 3:08
add a comment |
You pass the onDelete
function as a prop to your Tweets component but not to your Tweet component. Your Tweet component tries to call it in the handleDelete function. To fix this, pass the prop onto the Tweet component.
render() {
let that = this
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} onDelete={that.props.onDelete} />;
});
return (
<div>
{tweets}
</div>
);
}
Thank you for your reply. I just modified my code just like the way you showed and I'm now gettingUncaught TypeError: Cannot read property 'props' of undefined
...
– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors inonSubmitDelete
ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:41
add a comment |
You pass the onDelete
function as a prop to your Tweets component but not to your Tweet component. Your Tweet component tries to call it in the handleDelete function. To fix this, pass the prop onto the Tweet component.
render() {
let that = this
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} onDelete={that.props.onDelete} />;
});
return (
<div>
{tweets}
</div>
);
}
Thank you for your reply. I just modified my code just like the way you showed and I'm now gettingUncaught TypeError: Cannot read property 'props' of undefined
...
– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors inonSubmitDelete
ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:41
add a comment |
You pass the onDelete
function as a prop to your Tweets component but not to your Tweet component. Your Tweet component tries to call it in the handleDelete function. To fix this, pass the prop onto the Tweet component.
render() {
let that = this
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} onDelete={that.props.onDelete} />;
});
return (
<div>
{tweets}
</div>
);
}
You pass the onDelete
function as a prop to your Tweets component but not to your Tweet component. Your Tweet component tries to call it in the handleDelete function. To fix this, pass the prop onto the Tweet component.
render() {
let that = this
const tweets = this.props.tweets.map(function (tweet) {
return <Tweet tweet={tweet} key={tweet.id} onDelete={that.props.onDelete} />;
});
return (
<div>
{tweets}
</div>
);
}
edited Nov 21 at 2:29
answered Nov 21 at 2:04
Leo Farmer
5,57032135
5,57032135
Thank you for your reply. I just modified my code just like the way you showed and I'm now gettingUncaught TypeError: Cannot read property 'props' of undefined
...
– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors inonSubmitDelete
ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:41
add a comment |
Thank you for your reply. I just modified my code just like the way you showed and I'm now gettingUncaught TypeError: Cannot read property 'props' of undefined
...
– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors inonSubmitDelete
ajax part but my original problem is solved! Thank you so much.
– ta539tg70
Nov 21 at 2:41
Thank you for your reply. I just modified my code just like the way you showed and I'm now getting
Uncaught TypeError: Cannot read property 'props' of undefined
...– ta539tg70
Nov 21 at 2:22
Thank you for your reply. I just modified my code just like the way you showed and I'm now getting
Uncaught TypeError: Cannot read property 'props' of undefined
...– ta539tg70
Nov 21 at 2:22
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
@ta539tg70 Updated to try and get correct scope for this.props
– Leo Farmer
Nov 21 at 2:31
It looks like I now have errors in
onSubmitDelete
ajax part but my original problem is solved! Thank you so much.– ta539tg70
Nov 21 at 2:41
It looks like I now have errors in
onSubmitDelete
ajax part but my original problem is solved! Thank you so much.– ta539tg70
Nov 21 at 2:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404236%2fgetting-uncaught-typeerror-in-a-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown