I am trying in Redux change state via action but when I try to fire up the action it won't return anything. I think there is SOME problem in return statement but not sure how it is. Everything before return works. If I put there any console.log it returns but if I put console.log after return it won't return anything.
I am calling setDir
through mapDispatchProps
.
I have try to look at some cases but can't find answer anywhere.
Action.js
export const setDir = (dir) => {
const newDirection = dir === "asc" ? "desc" : "asc";
return function (dispatch) {
console.log(newDirection);
dispatch({ type: SET_DIR, payload: newDirection });
};
};
index.js
const sortTable = (column) => {
setColmn("val");
setDir("desc");
};
<View style={styles.tableHeader}>
{columns.map((column, index) => {
{
return (
<TouchableOpacity
key={index}
style={styles.columnHeader}
onPress={() => sortTable(column)}
>
<Text style={styles.columnHeaderTxt}>
{column + " "}
{selectedColumn === column && (
<AntDesign
size={20}
name={direction === "desc" ? "arrowdown" : "arrowup"}
/>
)}
</Text>
</TouchableOpacity>
);
}
})}
</View>
const mapDispatchProps = (dispatch) =>
bindActionCreators({ fetchVoziky, setColmn, setDir }, dispatch);
setDir
? Please include the relevant part of your code.setDir
throughmapDispatchProps
". You are calling it insortTable
. It's obvious now why it "doesn't work":setDir
returns a function that needs be called withdispatch
as argument. You are callingsetDir(...)
but are not doing anything with its return value (i.e. you are creating a function and are not calling it, so of course whatever code is inside the function is not executed).props.
beforesetDir
. Thank you @FelixKling