gpt4 book ai didi

javascript - React Router Link onClick redux Action 调用不调用 reducer

转载 作者:行者123 更新时间:2023-11-30 21:11:53 24 4
gpt4 key购买 nike

在更新新状态数据并在显示的新组件中呈现之前,使用 React 路由器的 Link 标签在“onClick”上传递一个 Action 。不调用 Reducer 或对状态进行任何更改。不知道为什么。任何帮助将不胜感激。

不记录“reducer working”的 Reducer。在主屏幕上记录“reducer called”

import GATHER_NEIGHBOURS from '../actions';
import GATHER_REGION from '../actions';
import GATHER_ALL from '../actions';

import _ from 'lodash';


const displayData = (state = {}, action) => {
console.log("reducer called!");
switch (action.type) {
case 'GATHER_NEIGHBOURS':
console.log("reducer working!");
return { display: 'neighbours' };
case 'GATHER_REGION':
return {};
case 'GATHER_ALL':
return {};
}
return state;
};

export default displayData;

记录“工作”的操作

export const GATHER_NEIGHBOURS = "GATHER_NEIGHBOURS";

export function importNeighbourData (data) {
console.log('action working');
return {
type: GATHER_NEIGHBOURS,
payload: data
};
}

带有触发的 onClick 的容器

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { importNeighbourData, importRegionData, importAllData } from '../actions';


class HomeMenu extends Component {
constructor(props){
super(props);
this.handleClick50 =this.handleClick50.bind(this);
this.handleClick200 =this.handleClick200.bind(this);
this.handleClickAll =this.handleClickAll.bind(this);
}

handleClick50(e) {
console.log('click called');
importNeighbourData(this.props.planets);
}

handleClick200(e) {
importRegionData(this.props.planets);
}

handleClickAll(e) {
importAllData(this.props.planets);
}

render(){
return (
<div className="homeMenu">
<div>
<Link to="/browse" onClick={this.handleClick50}>
<img alt="earth-and-moon-icon" src="imgs/earth-and-moon.png"></img>
Neighbourhood<br/>
(less than 50 parsecs)
</Link>
</div>
<div>
<Link to="/browse" onClick={this.handleClick200}>
<img alt="solar-system-icon" src="imgs/solar-system.png"></img>
Regional<br/>
(less than 200 parsecs)
</Link>
</div>
<div>
<Link to="/browse" onClick={this.props.handleClickAll}>
<img alt="galaxy-icon" src="imgs/galaxy-view.png"></img>
All
</Link>
</div>
</div>
);
}
}

function mapStateToProps({ planets }, ownProps) {
return { planets };
}

function mapDispatchToProps(dispatch, ownProps) {
return bindActionCreators({importNeighbourData, importRegionData, importAllData}, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(HomeMenu);

最佳答案

在你的代码中,你正在调用

importNeighbourData();
importRegionData();
importAllData();

其实应该这样称呼,

this.props.importNeighbourData();
this.props.importRegionData();
this.props.importAllData();

因为,redux 只会监听调度绑定(bind)操作,所以要启用它,我们使用 connect来自 react-redux 的函数包,这样 redux 就会知道在操作执行时更新自己。

connect会将您的操作绑定(bind)到 this.props 上的组件 Prop , 所以使用 this.props.action() 是非常必要的而不仅仅是 action()

The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch or the Redux store to it.

关于javascript - React Router Link onClick redux Action 调用不调用 reducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46047191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com