react服务端渲染一定要用redux管理数据吗?可以直接写在组件中吗?
因为 redux 学的不好,写起来感觉比较耗时,现在需要快速完成一个react 的spa 到服务端渲染的同构,不用redux可以吗?
以下是我原来的SPA代码
import React, { Component } from 'react';
import { Link } from "react-router-dom";
import './index.less';
import channelId from '../../config/channel'
import host from '../../config/host'
import TitleText from '../TitleText';
export default class ProfessorList extends Component {
constructor(props){
super(props);
this.state = {
professorList: [],
};
this.fetchProfessorList=this.fetchProfessorList.bind(this);
}
fetchProfessorList(){
let _this=this;
fetch(`${host}/web/pub/list_pub_in_channel?channelId=${channelId}&page=0&size=6`,{
method:'GET',
mode:'cors',
}).then(function(response){
return response.json().then(function(res){
_this.setState({
professorList:res.content
});
});
}).then(function(res){
if(res){
console.log(res);
}
});
}
componentDidMount(){
this.fetchProfessorList();
}
render() {
let professorList=this.state.professorList;
return (
<div className="professorList">
<TitleText headerName={'专家列表'}/>
<ul>
{
professorList.map((value, index) => {
return (
<li key={index}>
<Link to={"/news_list/professor/"+value.pubId}>
<div className="avatarBox">
<img src={value.avatarUrl} alt=""/>
</div>
<div className="professor-name">{value.name}</div>
<div className="professor-introduction">{value.introduction}</div>
</Link>
</li>)
})
}
</ul>
<Link to="/professor_list">
<div className="btn-more">
查看更多
</div>
</Link>
</div>
);
}
}
1 回答946 阅读✓ 已解决
1 回答604 阅读✓ 已解决
2 回答781 阅读
当然可以啊,
redux
和react
是独立的库,可以随意组合,又不是专门为react
写的,你可以直接写成变量啊接着调用
反正对后端来说,前端的
JS
、html
、css
都不过是字符串而已