0

I am trying to build a search bar component that will later make an api call and store the results in redux. however I am having trouble getting the onChange method working properly. My text onscreen doesn't change, but I can see in the console that it keeps printing out the initial state plus the last letter I entered. I did some console logs but I can't seem to get my searchReducer to run at all.

// Searchbar.js

const Searchbar = ({ query, results }) => {
  const onSubmit = (e) => {
    e.preventDefault();
  };

  const onChange = (e) => {
    UpdateQuery(e.target.value);
  };

  return (
    <form onSubmit={onSubmit}>
      <label htmlFor="search">Search Bar</label>
      <input
        className="search-input"
        name="search"
        type="text"
        placeholder="Search Meals..."
        value={query}
        onChange={onChange}
      />
      <input className="search-btn" type="submit" value="Search" />
    </form>
  );
};

const mapStateToProps = (state) => {
  return {
    query: state.search.query,
    results: state.search.results,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    UpdateQuery: (query) => dispatch(UpdateQuery(query)),
    UpdateResults: (results) => dispatch(UpdateResults(results)),
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);
// search-actions.js

import * as actionTypes from "./search-types";

export const UpdateQuery = (query) => {
  console.log("query >>> " + query);
  return {
    type: actionTypes.UPDATE_QUERY,
    payload: query,
  };
};

export const UpdateResults = (results) => {
  console.log("results >>> " + results);
  return {
    type: actionTypes.UPDATE_RESULTS,
    payload: results,
  };
};
// search-reducer.js 

import * as actionTypes from "./search-types";

const INITIAL_STATE = {
  query: "test",
  results: ['test'],
};

const searchReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case actionTypes.UPDATE_QUERY:
      return {
        ...state,
        query: action.payload,
      };

    case actionTypes.UPDATE_RESULTS:
      return {
        ...state,
        results: action.payload,
      };
    default:
      return state;
  }
};

export default searchReducer;

// rootReducer.js

const rootReducer = combineReducers({
    shop: shopReducer,
    search: searchReducer,
});

export default rootReducer;

2 Answers 2

1

You need to destructure it

const Searchbar = ({ query, results, UpdateQuery, UpdateResults }) => {

Similar to mapStateToProps the mapDispatchToProps will be available to the connected Component as a Props

Sign up to request clarification or add additional context in comments.

1 Comment

thank you! Can't believe I missed something so simple.
1

You would need to get the UpdateQuery and UpdateResults from the props and not directly use them for this to work like so :-

const Searchbar = ({ query, results,UpdateQuery, UpdateResults  }) => {
  const onSubmit = (e) => {
    e.preventDefault();
  };

  const onChange = (e) => {
    UpdateQuery(e.target.value);
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.