简体   繁体   English

使用React / Redux动态更新数据

[英]Dynamically updating data with React/Redux

I need to update data every 5 seconds, how can I do it with Redux actions? 我需要每5秒钟更新一次数据,如何使用Redux操作进行更新? Now I use this approach, but Im not sure that it is good idea. 现在,我使用这种方法,但是我不确定这是个好主意。 Maybe there are better ways to do it? 也许有更好的方法可以做到? Can I do it in actions? 我可以采取行动吗? Thanks for answers and sorry for my English) 感谢您的回答,对不起我的英语)

class Example extends Component {   
constructor(props) {
super(props);
this.interval = null;
this.state = {
  data: props.data
  }
}

componentDidMount() {
 this.updateData(this.props.id)
}


updateData = currentId => {
this.interval = setInterval(() => {
  this.props.getData(currentId);
}, 5000);
}

componentWillUnmount() {
 const self = this;
 clearInterval(self.interval);
}

componentWillReceiveProps(nextProps) {
  if (this.props.data!== nextProps.data) {
   this.setState({data: nextProps.data});
  } else if(this.props.id !== nextProps.id) {
   this.updateData(nextProps.id)
  }
}  
render() {return(...html here)}
}

export default connect(
 store => ({
data: store.data,
 }),
 {getData}
)(Example);

I think what you have is good in terms of the approach of setting an interval. 我认为就间隔而言,您所拥有的是好的。 However, there is no need to copy your props.data into state. 但是,无需将props.data复制到状态中。 You can remove the local state, and in your render method, you can directly reference props.data and it will re-render every time the prop changes. 您可以删除本地状态,并且在您的render方法中,您可以直接引用props.data并且每次prop更改时它将重新渲染。 If you want this interval to happen continuously through the lifetime of your app, you can move it to a higher level in the component hierarchy, if you want. 如果希望此间隔在应用程序的整个生命周期中连续发生,则可以根据需要将其移至组件层次结构中的更高级别。

I've rewritten your snippet a little bit. 我已经对您的代码段进行了一些重写。 Are you sure you need to copy props into the state of the component? 您确定需要将props复制到组件state吗? If you're just rendering based on the props, and the state is never changed, you don't have to do this. 如果您只是基于道具进行渲染,并且状态从未改变,则无需执行此操作。

I'm also assuming that getData in your code is a redux action creator, defined somewhere else. 我还假设您代码中的getData是Redux动作创建者,在其他地方定义。

The setInterval logic you wrote is mostly correct I'd say. 我会说,您编写的setInterval逻辑基本上是正确的。 You want to dispatch a Redux action every 5 seconds, as long as the component lives. 您希望每5秒调度一次Redux操作,只要组件有效即可。 Fair enough. 很公平。

class Example extends Component {
    constructor(props) {
        super(props);

        this.interval = null;
    }

    componentDidMount() {
        this.pollForData(this.props.id)
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.id !== nextProps.id) {
            this.pollForData(nextProps.id);
        }
    }

    componentWillUnmount() {
        clearInterval(this.interval);
    }

    pollForData(id) {
        // First clear old interval if one exists
        if (this.interval) {
            clearInterval(this.interval);
        }
        this.interval = setInterval(
            () => this.props.getData(id),
            5000
        );
    }

    render() {
        return (...html here)
    }
}

const mapStateToProps = state => ({
    data: state.data,
    id: // ? was this missing in your code? where is this.props.id coming from?
});

const mapDispatchToProps = dispatch => ({
    getData // I'm assuming you defined this somewhere else
});

export default connect(mapStateToProps, mapDispatchToProps(Example);

how can I do it with Redux actions? 我该如何使用Redux操作?

Use redux async action. 使用redux async操作。

install redux thunk . 安装redux thunk

this.interval = setInterval(() => {
    return dispatch => {
        dispatch({
            type: "GET_DATA",
            Id: currentId
        })
    }
}, 5000);

In REDUCERS class,define one more action type GET_DATA which will call 在REDUCERS类中,再定义一个操作类型GET_DATA ,它将调用

getData with above signature. 具有上述签名的getData

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM