Javascript 依赖于异步请求和状态更改的React呈现方法
我正在努力学习ReactJS和Redux,遇到了一个我似乎无法克服的问题 我有一个React组件,它从异步请求中获取数据Javascript 依赖于异步请求和状态更改的React呈现方法,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在努力学习ReactJS和Redux,遇到了一个我似乎无法克服的问题 我有一个React组件,它从异步请求中获取数据 export class MyPage extends React.Component { constructor(props) { super(props) this.state = { enableFeature: false, } this.handleEnableFeatureChange = this.handleE
export class MyPage extends React.Component {
constructor(props) {
super(props)
this.state = {
enableFeature: false,
}
this.handleEnableFeatureChange = this.handleEnableFeatureChange.bind(this)
}
componentWillMount () {
this.fetchData()
}
fetchData () {
let token = this.props.token
this.props.actions.fetchData(token)
}
handleEnableFeatureChange (event) {
this.setState({ enableFeature: event.target.checked })
}
render () {
if (this.props.isFetching) {
return (
<div>Loading...</div>
)
} else {
return (
<div>
<label>Enable Feature
<input type="checkbox"
className="form-control"
checked={this.props.enableFeature}
onChange={this.handleEnableFeatureChange}
/>
</label>
</div>
)
}
}
}
导出类MyPage扩展React.Component{
建造师(道具){
超级(道具)
此.state={
enableFeature:false,
}
this.handleEnableFeatureChange=this.handleEnableFeatureChange.bind(this)
}
组件将安装(){
this.fetchData()
}
获取数据(){
让令牌=this.props.token
this.props.actions.fetchData(令牌)
}
handleEnableFeatureChange(事件){
this.setState({enableFeature:event.target.checked})
}
渲染(){
if(this.props.isFetching){
返回(
加载。。。
)
}否则{
返回(
启用功能
)
}
}
}
所以,我现在的问题是,当我更改复选框的状态时,我想更新数据的状态。但是,每次我更新数据的状态时,react component方法shouldComponentUpdate
就会启动,并使用当前的道具渲染原始数据
我想看看这些案件一般如何处理
谢谢。将其更改为
checked={this.state.enablefeact}
将其更改为checked={this.state.enablefeact}
尝试如下操作,即:
export class MyPage extends React.Component {
static propTypes = {
isFetching: React.PropTypes.bool,
enableFeature: React.PropTypes.bool,
token: React.PropTypes.string,
actions: React.PropTypes.shape({
fetchData: React.PropTypes.func
})
};
state = {
enableFeature: false,
};
componentWillMount () {
this.fetchData();
}
/* Assign received prop to state, so that this state can be used in render */
componentWillReceiveProps(nextProps) {
if (this.props.isFetching && !nextProps.isFetching) {
this.state.enableFeature = nextProps.enableFeature;
}
}
fetchData () {
const { token } = this.props;
this.props.actions.fetchData(token)
}
handleEnableFeatureChange = (event) => {
this.setState({ enableFeature: event.target.checked })
};
render () {
return (<div>
{ this.props.isFetching && "Loading..." }
{
!this.props.isFetching && <label>
Enable Feature
<input
type="checkbox"
className="form-control"
checked={this.state.enableFeature}
onChange={this.handleEnableFeatureChange}
/>
</label>
}
</div>);
}
}
导出类MyPage扩展React.Component{
静态类型={
isFetching:React.PropTypes.bool,
enableFeature:React.PropTypes.bool,
标记:React.PropTypes.string,
操作:React.PropTypes.shape({
fetchData:React.PropTypes.func
})
};
状态={
enableFeature:false,
};
组件将安装(){
这是fetchData();
}
/*将收到的道具指定给状态,以便可以在渲染中使用此状态*/
组件将接收道具(下一步){
if(this.props.isFetching&!nextrops.isFetching){
this.state.enableFeature=nextrops.enableFeature;
}
}
获取数据(){
const{token}=this.props;
this.props.actions.fetchData(令牌)
}
handleEnableFeatureChange=(事件)=>{
this.setState({enableFeature:event.target.checked})
};
渲染(){
返回(
{this.props.isFetching&“Loading…”
{
!this.props.isFetching&&
启用功能
}
);
}
}
注意:上面的代码没有被执行,但是应该可以工作(巴贝尔的0阶段代码)试着像下面这样做,即
export class MyPage extends React.Component {
static propTypes = {
isFetching: React.PropTypes.bool,
enableFeature: React.PropTypes.bool,
token: React.PropTypes.string,
actions: React.PropTypes.shape({
fetchData: React.PropTypes.func
})
};
state = {
enableFeature: false,
};
componentWillMount () {
this.fetchData();
}
/* Assign received prop to state, so that this state can be used in render */
componentWillReceiveProps(nextProps) {
if (this.props.isFetching && !nextProps.isFetching) {
this.state.enableFeature = nextProps.enableFeature;
}
}
fetchData () {
const { token } = this.props;
this.props.actions.fetchData(token)
}
handleEnableFeatureChange = (event) => {
this.setState({ enableFeature: event.target.checked })
};
render () {
return (<div>
{ this.props.isFetching && "Loading..." }
{
!this.props.isFetching && <label>
Enable Feature
<input
type="checkbox"
className="form-control"
checked={this.state.enableFeature}
onChange={this.handleEnableFeatureChange}
/>
</label>
}
</div>);
}
}
导出类MyPage扩展React.Component{
静态类型={
isFetching:React.PropTypes.bool,
enableFeature:React.PropTypes.bool,
标记:React.PropTypes.string,
操作:React.PropTypes.shape({
fetchData:React.PropTypes.func
})
};
状态={
enableFeature:false,
};
组件将安装(){
这是fetchData();
}
/*将收到的道具指定给状态,以便可以在渲染中使用此状态*/
组件将接收道具(下一步){
if(this.props.isFetching&!nextrops.isFetching){
this.state.enableFeature=nextrops.enableFeature;
}
}
获取数据(){
const{token}=this.props;
this.props.actions.fetchData(令牌)
}
handleEnableFeatureChange=(事件)=>{
this.setState({enableFeature:event.target.checked})
};
渲染(){
返回(
{this.props.isFetching&“Loading…”
{
!this.props.isFetching&&
启用功能
}
);
}
}
注意:上面的代码没有执行,但是如果您使用的是Redux,应该可以工作(babel的stage-0代码)。。读这个。。。如果要使用React组件事件,请尝试返回false,直到达到所需的状态。如果使用Re,请将this
this.setState({enablefeact:event.target.checked})
更改为this.setState((prevState)=>({enablefeact:!prevState.enablefeact})