0

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);
4
  • How exactly are you using setDir? Please include the relevant part of your code. Commented Sep 23, 2022 at 11:24
  • @FelixKling sorry. Now its updated
    – fikus
    Commented Sep 23, 2022 at 11:34
  • 2
    It doesn't look like you are "calling setDir through mapDispatchProps". You are calling it in sortTable. It's obvious now why it "doesn't work": setDir returns a function that needs be called with dispatch as argument. You are calling setDir(...) 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). Commented Sep 23, 2022 at 11:37
  • yeah u had right. I forget to put props. before setDir. Thank you @FelixKling
    – fikus
    Commented Sep 23, 2022 at 11:45

1 Answer 1

0

Hii,
You should try to put it
const newDirection = dir === "asc" ? "desc" : "asc";
before the console.log or inside the return.

2
  • And why do you think would that solve the problem? Commented Sep 23, 2022 at 11:22
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 27, 2022 at 13:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.