我是React,Redux和JS的新手。我想知道如何在组件中链接操作?在列表屏幕上,我应用程序检索用户的地理位置,然后使用当前的long和lat获取api url。
我超时完成了。但我认为这是一种冒险的方式。
import {
aFetch,
aGetPosition,
} from '../../actions';
import { API_URL } from '../../config/ApiEndpoints';
class App extends Component {
componentDidMount() {
this.props.aGetPosition();
setTimeout(() => {
const { latitude, longitude } = this.props.position.data.coords;
const url = `${API_URL}=${latitude},${longitude}`;
this.props.aFetch(url);
}, 1000);
}
render(){...}
}
const mapStateToProps = (state) => {
return {
position: state.position,
items: state.items,
};
};
export default connect(mapStateToProps, { aFetch, aGetPosition })(App);是否可以使用.then()或类似的方法来实现?
发布于 2017-01-18 06:16:19
我相信你需要componentWillReceiveProps()方法。docs很好地解释了它以及组件生命周期中的其他方法。
发布于 2017-01-18 11:25:29
首先,你需要分派你的redux操作。这可以通过使用mapDispatchToProps函数来实现,该函数将是您的connect函数中的第二个参数(而不是您现在所拥有的带有操作的普通对象)。关于在React容器中实现redux的文档,包括从容器中分派操作:http://redux.js.org/docs/basics/UsageWithReact.html#implementing-container-components
要处理异步操作和链接操作,您可能希望使用中间件来帮助您,例如redux-thunk。文档链接:http://redux.js.org/docs/advanced/AsyncActions.html#async-action-creators (我建议阅读整个页面,但链接将直接转到redux-thunk部分)。
https://stackoverflow.com/questions/41707904
复制相似问题