2

i'm trying to split my input values with comma at onBlur function and set them to redux state as array but it's not working.

I read it at some topics that possible with .split(',')

my approach:

input onBlur={event = () => this.props.getValue(event.target.value)} 

my action:

export const getValue = (value) => {
  return dispatch => {
    dispatch({
      type: 'SET_VALUE',
      payload: value.split(',')
    });
  };
};

my reducer:

let initialState = {
  codes: [],
};

export default (state = initialState, action) => {
  switch (action.type) {
    case SET_VALUE:
      return { ...state, codes: action.payload };
    default:
      return state;
  }
};

what i want at the end? :

i want in my reducer like that data:

codes: [28282, 28922, 18171, 27272, ....]

but it give me now something like that:

codes: [28282 28922 18171 27272 ....]
10
  • is it giving you a long string instead of an array ? like ["28282 28922 18171 27272"] ? Commented Aug 13, 2019 at 12:56
  • yes it's giving to me it Commented Aug 13, 2019 at 12:56
  • and the values you are entering are space-separated, not comma-separated? Commented Aug 13, 2019 at 12:57
  • then just do split(' ') Commented Aug 13, 2019 at 12:57
  • yes i press to space myself Commented Aug 13, 2019 at 12:57

1 Answer 1

1

If you want an array the action should be like this

export const getValue = (value) => {
  return dispatch => {
    dispatch({
      type: 'SET_VALUE',
      payload: value.split(' ')
    });
  };
};
Sign up to request clarification or add additional context in comments.

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.