【发布时间】:2020-01-21 18:27:31
【问题描述】:
我一直在尝试,我的组件不会重新渲染自己。下面是我的减速器代码,我已经尽一切努力不改变状态。在我的组件代码中,在渲染方法中,我有一个日志语句 console.log("Check Here");我知道组件不会重新渲染,因为此日志在组件第一次渲染时工作,但在 reducer 更改状态后,日志语句没有被调用。在日志中,我可以清楚地看到上一个状态和下一个状态只是我正在更改的一个 SearchType 不同。请帮忙!!
const initState = {
searchType: ""
};
const techniqueReducer = (state = initState, action) => {
switch (action.type) {
case actionTypeConstants.GET_SEARCH:
{
return { ...state, searchType: "new string" };
}
default: {
return state;
}
}
};
export default myReducer;
我的组件代码如下
import React, { Component } from "react";
import { connect } from "react-redux";
import * as tDispatchers from "../actions/Actions";
const mapStateToProps = state => {
return {
searchType: state.searchType
};
};
class SearchCollection extends Component {
Search= () => {
this.props.dispatch(tDispatchers.getSearch(document.getElementById("txtSearch").value));
}
render() {
console.log("Check Here")
return (
<div class="container-fluid">
<div>
<input
type="text"
id="txtSearch"
class="form-control"
placeholder="Enter Search Keywords Here..."
/>
</div>
<div>
<button
className="btn btn-light btn-sm m-1"
onClick={this.Search}
>
Search
</button>
</div>
</div>
);
}
}
export default connect(mapStateToProps)(SearchCollection);
GetSearch 如下所示 我计划最终将有效负载传递给减速器,但目前我没有
import * as actionTypeConstants from "../action_type_constants";
import axios from "axios";
export function getSearch(searchtext) {
return dispatchFunction => {
axios
.get("<api call>"+searchtext)
.then(response => {
dispatchFunction({
type: actionTypeConstants.GET_SEARCH,
payload: response.data.data
});
})
};
}
ActionTypeConstant
export const GET_SEARCH = "GET_SEARCH";
【问题讨论】:
-
return { ...state, SearchType: "new string" };拼写不好?搜索类型?
-
你能发布有问题的组件代码吗?请包括状态和调度代码的映射。
-
我已经编辑了这个问题..当我稍微修改代码以发布到这里时发生大写错误
-
我添加了组件代码
-
@kimi86 能否在 sn-p 中也包含
getSearchTechniques和actionTypeConstants.GET_SEARCH? :)
标签: reactjs react-native redux react-redux