做一个消息评论列表,针对具体某一个消息,里面有“收藏”和“点赞”功能,但是发现直接修改对象的树形,无法改变视图,因此做了笔记,方便后续学习
循环遍历所有的对象,然后修改对应的值,重新设置 数组对象
import * as React from 'react';
import {
Icon,
Button,
Input,
Form,
} from 'antd';
import styles from './userinfo.less';
import logo from '/static/logo.svg';
import Connection from '@/store';
import Router from 'next/router';
@Connection(({albums, loading}) => ({
albums: albums,
loading: loading.effects['_user/login'],
}))
export default class UserInfo extends React.Component {
state = {
isEdit: false
static async getInitialProps(props) {
return {
username: 'Yijun Liu',
address: 'USA. University of San Francisco',
major: 'Computer Scientis',
email: 'zhangsan@163.com',
"messageList": [
id: 1,
email: 'Yijun Liu@163.com',
message: 'Just setting up my Twitter.111111',
date: '2019-5-18 11:33:56'
id: 2,
email: 'Yijun Liu@163.com',
message: 'Just setting up my Twitter.222222',
date: '2019-5-18 11:33:56'
id: 3,
email: 'Yijun Liu@163.com',
message: 'Just setting up my Twitter.333333',
date: '2019-5-18 11:33:56'
constructor(props) {
super(props);
let {username} = this.props;
console.log('huangbiao', username)
componentDidMount () {
document.getElementsByTagName('body')[0].style.background = '#E7ECEF';
let {username, address, major, email, messageList} = this.props;
console.log('huangbiao', username)
this.setState({
"username": username,
"address": address,
"major": major,
"email": email,
"messageList": messageList
componentWillUnmount () {
goHomePage () {
Router.push('/profile');
collectAction (messageObj, eventObj) {
let { messageList } = this.state;
let newListData = messageList.map(function(item, idx){
if (item.id === messageObj.id) {
return {
...item,
isCollect: !messageObj.isCollect
} else {
return item;
});
this.setState({
messageList: newListData
complimentAction(messageObj, index, eventObj) {
let { messageList } = this.state;
messageList[index]['isCompliment'] = !messageList[index]['isCompliment'];
let newListData = messageList;
this.setState({
messageList: newListData
getMessageListHtml () {
const that = this;
let { messageList } = this.state;
if (!messageList) {
messageList = [];
return messageList.map(function(messageObj, index){
return (
<div className={styles['message-container']} key={messageObj.id}>
<div className={styles.portrait}>
[外链图片转存失败(img-aFNgwwai-1562046300900)(https://mp.csdn.net/static/logo.png)]
</div>
<div className={styles['info-container']}>
<div className={styles.author}>
{messageObj.email}
</div>
<div className={styles.theme}>
{messageObj.message}
</div>
<div className={styles.time}>
{messageObj.date}
<div className={styles['tool-bar']}>
<div className={styles['bar-btn']}>
{}
messageObj.isCompliment ?
<Icon type="heart" onClick={that.complimentAction.bind(that, messageObj, index)} style={{color: 'red'}} />
: <Icon type="heart" onClick={that.complimentAction.bind(that, messageObj, index)}/>
</div>
<div className={styles['bar-btn']}>
<Icon type="message" onClick={that.collectAction.bind(that, messageObj)}/>
</div>
<div className={styles['bar-btn']}>
{}
messageObj.isCollect ?
<Icon type="star" onClick={that.collectAction.bind(that, messageObj)} style={{color: '#FEC603'}} />
: <Icon type="star" onClick={that.collectAction.bind(that, messageObj)}/>
</div>
</div>
</div>
</div>
</div>
render() {
let messageListHtml = this.getMessageListHtml();
return (
<div className={styles['userinfo-page']}>
<div className={styles['main-container']}>
<div className={styles['main-center']}>
{messageListHtml}
</div>
</div>
</div>
文章目录问题描述解决办法案例说明问题描述做一个消息评论列表,针对具体某一个消息,里面有“收藏”和“点赞”功能,但是发现直接修改对象的树形,无法改变视图,因此做了笔记,方便后续学习解决办法循环遍历所有的对象,然后修改对应的值,重新设置 数组对象案例说明import * as React from 'react';import { Icon, Button, In...
chrome浏览器不支持webgl(webgl context must not be null, please enable webgl in your browser settings!)
墨茗棋緲: