在渲染容器之前反应 redux 调度操作
React redux dispatch action before render container
我对 React-redux 应用程序开发还很陌生,我想了解如何在页面加载后立即分派另一个操作。以下是我的容器代码。我正在使用这个 (https://github.com/jpsierens/webpack-react-redux) 样板文件。
let locationSearch;
const ActivationPage = ({activateUser}) => {
return (
<div className="container">
<h2>Activation Required</h2>
<p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
{ activateUser() }
</div>
);
};
ActivationPage.propTypes = {
activateUser: PropTypes.func
};
const mapStateToProps = (state) => {
return {
message: state.message,
currentUser: state.currentUser,
request: state.request
};
};
const mapDispatchToProps = (dispatch) => {
return {
activateUser: () => {
console.log(location);
if (location.search !== '') {
locationSearch = querystring.parse(location.search.replace('?', ''));
console.log(locationSearch);
if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) {
// change request state to pending to show loader
dispatch(actions.requestActions.requestPending());
}
}
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ActivationPage);
此代码给我 警告:setState(...):无法在现有状态转换期间更新 可能是因为我在渲染函数 (IDK) 期间调度了一个动作。如何转换给定代码以在页面加载后立即自动触发 activateUser() 函数。
https://facebook.github.io/react/docs/react-component.html
componentWillMount()
和 componentDidMount()
给你。
我的意见 - 你应该避免使用 componentWillMount
而更喜欢 componentDidMount
- 如果你在某个时候将使用服务器渲染
,这是有道理的
在我将组件转换为 class
extends Component
之后它开始工作了这个答案帮助我理解了一些概念
以下是为那些可能和我一样对此感到困惑的人的实施。
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import actions from '../actions';
let locationSearch;
class ActivationPage extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.props.activateUser();
}
render() {
return (
<div className="container">
<h2>Activation Required</h2>
<p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
</div>
);
}
}
ActivationPage.propTypes = {
activateUser: PropTypes.func
};
const mapStateToProps = (state) => {
return {
request: state.request
};
};
const mapDispatchToProps = (dispatch) => {
return {
activateUser: () => {
if (location.search !== '') {
locationSearch = querystring.parse(location.search.replace('?', ''));
if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) {
// change request state to pending to show loader
dispatch(actions.requestActions.requestPending());
}
}
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ActivationPage);
我对 React-redux 应用程序开发还很陌生,我想了解如何在页面加载后立即分派另一个操作。以下是我的容器代码。我正在使用这个 (https://github.com/jpsierens/webpack-react-redux) 样板文件。
let locationSearch;
const ActivationPage = ({activateUser}) => {
return (
<div className="container">
<h2>Activation Required</h2>
<p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
{ activateUser() }
</div>
);
};
ActivationPage.propTypes = {
activateUser: PropTypes.func
};
const mapStateToProps = (state) => {
return {
message: state.message,
currentUser: state.currentUser,
request: state.request
};
};
const mapDispatchToProps = (dispatch) => {
return {
activateUser: () => {
console.log(location);
if (location.search !== '') {
locationSearch = querystring.parse(location.search.replace('?', ''));
console.log(locationSearch);
if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) {
// change request state to pending to show loader
dispatch(actions.requestActions.requestPending());
}
}
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ActivationPage);
此代码给我 警告:setState(...):无法在现有状态转换期间更新 可能是因为我在渲染函数 (IDK) 期间调度了一个动作。如何转换给定代码以在页面加载后立即自动触发 activateUser() 函数。
https://facebook.github.io/react/docs/react-component.html
componentWillMount()
和 componentDidMount()
给你。
我的意见 - 你应该避免使用 componentWillMount
而更喜欢 componentDidMount
- 如果你在某个时候将使用服务器渲染
在我将组件转换为 class
extends Component
之后它开始工作了这个答案帮助我理解了一些概念
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import actions from '../actions';
let locationSearch;
class ActivationPage extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.props.activateUser();
}
render() {
return (
<div className="container">
<h2>Activation Required</h2>
<p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
</div>
);
}
}
ActivationPage.propTypes = {
activateUser: PropTypes.func
};
const mapStateToProps = (state) => {
return {
request: state.request
};
};
const mapDispatchToProps = (dispatch) => {
return {
activateUser: () => {
if (location.search !== '') {
locationSearch = querystring.parse(location.search.replace('?', ''));
if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) {
// change request state to pending to show loader
dispatch(actions.requestActions.requestPending());
}
}
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ActivationPage);