Reactjs 更新状态后,在react-redux中调用组件两次,从而再次获取初始状态,而不是更新的状态
这里的ShowFriends是我的单击事件,用于处理将状态标志设置为true的情况,如果这是true,我想显示组件,我将在Friends Reducer中设置为true。。。它正在启动,我可以在设置标志后设置标志。标志组件再次重新加载,状态再次设置回初始状态。我如何解决这个问题?我是新的反应和重复 这是我的减速机:Reactjs 更新状态后,在react-redux中调用组件两次,从而再次获取初始状态,而不是更新的状态,reactjs,redux,Reactjs,Redux,这里的ShowFriends是我的单击事件,用于处理将状态标志设置为true的情况,如果这是true,我想显示组件,我将在Friends Reducer中设置为true。。。它正在启动,我可以在设置标志后设置标志。标志组件再次重新加载,状态再次设置回初始状态。我如何解决这个问题?我是新的反应和重复 这是我的减速机: var initialState = { status: false, friends: [ { id: 1, name: 'satish', stat
var initialState = {
status: false,
friends: [
{ id: 1, name: 'satish', status: true },
{ id: 2, name: 'satish1', status: true },
{ id: 3, name: 'satish2', status: true },
{ id: 4, name: 'satish3', status: true },
{ id: 5, name: 'satish4', status: true }]
}
export const FriendsReducer = (state = initialState, action) => {
switch (action.type) {
case Types.ADD_FRIEND:
return Object.assign({}, state, { friends: addFriend(state.friends, action) });
case Types.GET_LIST:
return Object.assign({},state,state.status = action.isShow);
default:
return state;
}
}
export default FriendsReducer;
组成部分:
export class Add_A_Friend extends Component {
constructor(state) {
super();
this.props = state;
}
render() {
let Fname, Fcity;
return (
<div className="container">
<form className="form-signin">
<br /> <br /> <br />
<input type="text" className="form-control" placeholder="Friend Name" ref={x => Fname = x} />
<br />
<input type="text" className="form-control" placeholder="City Name" ref={x => Fcity = x} />
<br />
<button className="btn btn-primary" style={{ marginRight: 10 }} onClick={() => this.props.onAddFriend(Fname, Fcity)}>Add Friend</button>
<button className="btn btn-warning" style={{ float: 'right' }} onClick={() => this.props.onClear()}>Clear</button>
<button className="btn btn-info" onClick={() => this.props.onCheck(Fname)}>Check Name's</button>
<button className="btn btn-success" style={{ marginLeft: 10 }} onClick={() => this.props.onShowFriends(true)}>Show My Friends List</button>
{this.props.state.FriendsReducer.status && <Friends_List />}
</form>
</div>
);
}
}
Add_A_Friend.PropTypes = {
onAddFriend: PropTypes.func.isRequired,
onClear: PropTypes.func.isRequired,
onCheck: PropTypes.func.isRequired
}
const mapPropsToState = (state) => {
return {
state: state
}
};
export const mapDispatchToProps = (dispatch) => {
return {
onAddFriend: (Fname, Fcity) => {
dispatch(AddFriend(Fname.value, Fcity.value));
},
onClear: () => {
dispatch(ClearFriend());
},
onCheck: () => {
dispatch({ type: Types.CHECK_FRINDS });
},
onShowFriends: (isShow) => {
return dispatch({ type: Types.GET_LIST, isShow });
}
}
};
function AddFriend(name, city) {
return {
type: Types.ADD_FRIEND,
name, city
}
}
function ClearFriend() {
return { type: Types.CLEAR }
}
Add_A_Friend = connect(mapPropsToState, mapDispatchToProps)(Add_A_Friend);
export default Add_A_Friend;
导出类添加组件{
建造商(州){
超级();
this.props=状态;
}
render(){
让Fname,Fcity;
返回(
Fname=x}/>
Fcity=x}/>
this.props.onAddFriend(Fname,Fcity)}>addfriend
this.props.onClear()}>清除
this.props.onCheck(Fname)}>检查名称
this.props.onShowFriends(true)}>显示我的好友列表
{this.props.state.FriendsReducer.status&&}
);
}
}
添加朋友。属性类型={
onAddFriend:PropTypes.func.isRequired,
onClear:PropTypes.func.isRequired,
onCheck:PropTypes.func.isRequired
}
常量mapPropsToState=(状态)=>{
返回{
州:州
}
};
导出常量mapDispatchToProps=(调度)=>{
返回{
onAddFriend:(Fname,Fcity)=>{
分派(AddFriend(Fname.value,Fcity.value));
},
onClear:()=>{
分派(ClearFriend());
},
onCheck:()=>{
分派({type:Types.CHECK\u friends});
},
onShowFriends:(isShow)=>{
返回分派({type:Types.GET_LIST,isShow});
}
}
};
函数AddFriend(名称、城市){
返回{
type:Types.ADD\u FRIEND,
城市名称
}
}
函数ClearFriend(){
返回{type:Types.CLEAR}
}
添加好友=连接(mapPropsToState、mapDispatchToProps)(添加好友);
导出默认添加好友;
如果页面正在重新加载,那么当单击按钮时,您的表单似乎正在提交。你需要添加这个来防止它
onClick={(e) => { e.preventDefault(); this.props.onShowFriends(true)}}
如果页面正在重新加载,那么当点击按钮时,您的表单似乎正在提交。你需要添加这个来防止它
onClick={(e) => { e.preventDefault(); this.props.onShowFriends(true)}}
您不需要执行
this.props=state代码>在构造函数中。状态
已通过连接
映射到道具。所以,请删除它。谢谢,我删除并尝试了相同的结果。未捕获的类型错误:e.preventDeafult不是一个函数,结果将是相同的页面仍在重新加载检查我的答案,这是一个输入错误。它是e.preventDefault
您不需要执行this.props=state代码>在构造函数中。状态
已通过连接
映射到道具。所以,请删除它。谢谢,我删除并尝试了相同的结果。未捕获的类型错误:e.preventDeafult不是一个函数,结果将是相同的页面仍在重新加载检查我的答案,这是一个输入错误。它是e.preventDeafult
Uncaught类型错误:e.preventDeafult不是一个函数,仍然页面在重新加载应该是e.preventDeafult
e.preventDeafult
Uncaught类型错误:e.preventDeafult不是一个函数,仍然页面在重新加载应该是e.preventDeafult
不是e.preventDeafult