I'm making a simple twitter style clone in ReactJS and Redux. I currently have it that when you click on a post on the main timeline, it takes you to a page that renders its replies by taking the user to <Link to={/reply/${ling.id}}> and using [this.props.match.params.id] to render the right one. That works great using the mock-data I start with.
However, when a user posts a new post - and that gets successfully rendered on the timeline - something goes wrong and the parameters don't match up, and if you click on the post that should be taking you to reply/3, it is instead rendering the reply with an id of 2.
I can still access the new post, but I have to go to reply/0 (instead of reply/7, which it should be), and if I console log things I can see that the correct data is there, it's just not rendering correctly.
The console shows this before I update state:
http://localhost:3000/reply/2
LingReply.js:95 Should render ling # 2
LingReply.js:96 {id: 2 …}
But if I navigate to the same page after posting a new one, it does this:
LingReply.js:94 http://localhost:3000/reply/2
LingReply.js:95 Should render ling # 2
LingReply.js:96 {id: 1 …}
Here's the relevant code for the page that isn't working - am I missing something obvious?
import React, { Component } from "react";
import { Card, CardHeader, CardBody, CardFooter, Button } from "reactstrap";
import { Link } from "react-router-dom";
import { connect } from "react-redux";
class LingReply extends Component {
render() {
const ling = this.props.lings.lings[this.props.match.params.id];
console.log(window.location.href)
console.log("Should render ling #", this.props.match.params.id)
console.log(ling)
return (
<div key={ling.id}>
<Card className="ling mb-3">
<CardHeader>
<span className="ling-date">{ling.lingLang}</span>
<h3>
{ling.userIcon} - {ling.userName}
</h3>
</CardHeader>
<CardBody>{ling.lingBody}</CardBody>
</Card>
</div>
);
}
}
function mapStateToProps(state) {
return {
lings: state.lings,
};
}
export default connect(mapStateToProps)(LingReply);
Here it is on my GitHub: https://github.com/gordonmaloney/Lingr