React应该能够编辑映射数组中的一项,但是选择编辑时。它编辑数组中的所有项目。我将如何解决此问题,以便可以针对该特定post.id
进行编辑例如
我不太确定如何告诉您编辑特定项目的反应,这等同于ID。
PostList.js
import React, { Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import moment from 'moment';
import {connect} from 'react-redux';
import {DeletePost} from '../actions/';
import PostItem from './PostItem';
const Styles = {
myPaper: {
margin: '20px 0px',
padding: '20px'
}
}
class PostList extends Component{
constructor(props){
super(props);
this.state ={
isEditing:false
}
}
// Return a new function. Otherwise the DeletePost action will be dispatch each
// time the Component rerenders.
removePost = (id) => () => {
this.props.DeletePost(id);
}
onChange = (e) => {
e.preventDefault();
this.setState({
[e.target.title]: e.target.value
})
}
formEditing = (id) => ()=> {
if(this.state.isEditing){
this.setState({
isEditing: false
});
}
else{
this.setState({
isEditing:true
})
}
}
render(){
const {posts, editForm, isEditing, editChange} = this.props;
return (
<div>
{posts.map((post, i) => (
<Paper key={post.id} style={Styles.myPaper}>
<PostItem editForm={this.formEditing} isEditing={this.state.isEditing} removePost={this.removePost} {...post} />
</Paper>
))}
</div>
)
}
}
const mapDispatchToProps = (dispatch) => ({
// Pass id to the DeletePost functions.
DeletePost: (id) => dispatch(DeletePost(id))
});
export default connect(null, mapDispatchToProps)(PostList);
PostItem.js
import React, { Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import moment from 'moment';
import Editable from './Editable';
const Styles = {
myPaper: {
margin: '20px 0px',
padding: '20px'
}
}
const PostItem = ({ title, id, removePost, createdAt, post_content, username, editForm, isEditing, editChange}) => {
return(
<div>
<Typography variant="h6" component="h3">
{/* if else teneray operator */}
{/* when edit is clicked all items reveal an input field when it should just be the item that has */}
{isEditing ? (
<Editable editField={title} onChange={editChange}/>
): (
<div>
{title}
</div>
)}
</Typography>
<Typography component="p">
{post_content}
<h5>
by: {username}</h5>
<Typography color="textSecondary">{moment(createdAt).calendar()}</Typography>
</Typography>
{!isEditing ? (
<Button variant="outlined" type="submit" onClick={editForm(id)}>
Edit
</Button>
):(
<Button variant="outlined" type="submit" onClick={editForm(id)}>
Update
</Button>
)}
<Button
variant="outlined"
color="primary"
type="submit"
onClick={removePost(id)}>
Remove
</Button>
</div>
)
}
export default PostItem;
答案 0 :(得分:1)
在regexp_matches
中:
PostList.js
。
将{ isEditingId: null }
更改为以下内容:
formEditing
formEditing = (id) => ()=> {
this.setState({
isEditingId: id
});
}
存储到id
属性(是的,我必须将isEditingId
更改为isEditing
才有意义)在创建isEditingId
映射时,不使用PostItem
,现在检查isEditing={this.state.isEditing}
是否等于存储在post.id
中的帖子ID这个:isEditingId
。
isEditing={this.state.isEditingId === post.id}
仍作为布尔值传递给isEditing
,指示该帖子是否正在编辑。在PostItem.js
中:
PostItem.js
中帖子的id
传递回了editForm
。PostList
处理程序更改为onClick
。
onClick={editForm(null)}
中使isEditing
等于null
,因此您应该看不到任何PostList.js
元素。希望这对ya有用:)由于您没有包含Editable
文件,因此测试有点困难,但是希望上面的解释至少可以帮助您走上正确的轨道。