简体   繁体   中英

Can i send full store object from container to action creator (React/Redux)

Guys i don`t fully understand if this correct solution, but can i send some specific store Array[Object] from container to Action creator?

Task is to implement search in store.

Example of action creator

export const mySearch = (search) => {
return {
 type: SEARCH,
 a: data.text, //some additional value
 b: data.items //store object
}

You could apply some data from your store to the properties of your component.

const mapeStateToProps = state => ({ data: state.data });

From there you create your action by a "higher order function", passing your property in.

const mapDispatchToProps = dispatch => {
  const mySearch = data => search => dispatch({
    type: SEARCH,
    a: data.text,
    b: data.items,
  });

  return { mySearch };
};

Now call it from your component.

const Component = props =>
  <input type="search" onUpdate={props.mySearch(props.data)} />;

You can get the value of the input by your search parameter: search.target.value

If you're using redux-thunk, you could do it like

export const mySearch = (search) => {
return (dispatch, getState) => {

  const state = getState(); // <-- This will be your state tree
  const data = state.data; //  <-- or whatever path you have in your state

  // Now instead of return, dispatch it.
  dispatch({
    type: SEARCH,
    a: data.text,
    b: data.items
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM