Edit table values using react redux












2















I'm learning React-Redux and I'm stuck in a part of learning, I'm trying to edit the user value from table using the form that already exists, thereby taking the value of the line, and passing to the form, and also saving the user id in a variable or something of the type to update the user, I do not know how to proceed with that part



enter image description here



my actions: src/actions/user.js



import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';
import axios from 'axios';

const apiUrl = 'http://localhost:8080/api/v1';

export const createUser = ({ name, cpfcnpj }) => {
return (dispatch) => {
return axios.post(`${apiUrl}/users/`, { name, cpfcnpj })
.then(response => {
dispatch(createUserSuccess(response.data.data))
})
.catch(error => {
throw (error);
});
};
};

export const createUserSuccess = (data) => {
return {
type: ADD_USER,
payload: {
_id: data._id,
name: data.name,
cpfcnpj: data.cpfcnpj
}
}
};

export const deleteUserSuccess = id => {
return {
type: DELETE_USER,
payload: {
id
}
}
}

export const deleteUser = (id) => {
console.log(id)
return (dispatch) => {
return axios.delete(`${apiUrl}/users/${id}`)
.then(response => {
dispatch(deleteUserSuccess(id))
})
.catch(error => {
throw (error);
});
};
};

export const updateUserSuccess = (data) => {
return {
type: UPDATE_USER,
payload: {
_id: data._id,
name: data.name,
cpfcnpj: data.cpfcnpj
}
}
}

export const updateUser = (id, name, cpfcnpj) => {
console.log(id, name, cpfcnpj)
return (dispatch) => {
return axios.put(`${apiUrl}/users/${id}`, { name, cpfcnpj })
.then(response => {
dispatch(updateUserSuccess(response.data.data))
})
.catch(error => {
throw (error);
});
};
};

export const fetchUsers = (users) => {
return {
type: FETCH_USER,
users
}
};

export const fetchAllUsers = () => {
return (dispatch) => {
return axios.get(`${apiUrl}/users/`)
.then(response => {
dispatch(fetchUsers(response.data.data))
})
.catch(error => {
throw (error);
});
};
};


my Smart Component CreateUser: src/containers/CreateUser.js



import { connect } from 'react-redux';
import { createUser, updateUser } from '../actions/user';
import NewUser from '../components/NewUser';

const mapDispatchToProps = dispatch => {
return {
onAddUser: user => {
dispatch(createUser(user));
}
};
};

export default connect(
null,
mapDispatchToProps
)(NewUser);


my Dummy Component NewUser: src/components/NewUser.js



import React, { Component } from 'react';

class NewUser extends Component {
state = {
name: '',
cpfcnpj: '',
isEdit: false
};

handleInputChange = e => {
this.setState({
[e.target.name]: e.target.value
});
};

handleSubmit = e => {
e.preventDefault();
if (!this.state.isEdit) {
if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
this.props.onAddUser(this.state);
this.handleReset();
}
} else {
if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
this.props.onEdit(this.state);
this.handleReset();
}
}

};

handleReset = () => {
this.setState({
name: '',
cpfcnpj: ''
});
};

render() {
return (
<div>
<form className="form-inline" onSubmit={this.handleSubmit}>
<div className="form-group margin-right">
<input
type="text"
placeholder="Name"
className="form-control"
name="name"
onChange={this.handleInputChange}
value={this.state.name}
/>
</div>
<div className="form-group margin-right">
<input
type="text"
placeholder="CPF/CNPJ"
className="form-control"
name="cpfcnpj"
onChange={this.handleInputChange}
value={this.state.cpfcnpj}>
</input>
</div>
<div className="form-group">
<button type="submit" className={this.state.isEdit ? "btn btn-success margin-right hidden" : "btn btn-success margin-right"}>
<span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
&nbsp;
Adicionar
</button>
<button type="submit" className={this.state.isEdit ? "btn btn-primary margin-right" : "btn btn-primary margin-right hidden"}>
<span className="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>
&nbsp;
Salvar
</button>
<button type="button" className="btn btn-default margin-right" onClick={this.handleReset}>
<span className="glyphicon glyphicon-erase" aria-hidden="true"></span>
&nbsp;
Limpar
</button>
</div>
</form>
</div>
);
}
}

export default NewUser;


src/containers/UserList.js



import React from 'react';
import { connect } from 'react-redux';
import User from '../components/User';
import { deleteUser, updateUser } from '../actions/user';

function UserList({ users, onDelete, onEdit }) {
if (!users.length) {
return (
<div className="margin-top">
No Users
</div>
)
}
return (
<div className="margin-top">
<table className="table table-striped">
<thead>
<tr>
<th scope="col">Nome</th>
<th scope="col">CPF/CNPJ</th>
</tr>
</thead>
<tbody>
{users.map(user => {
return (
<User user={user} onDelete={onDelete} key={user._id} onEdit={onEdit} />
);
})}
</tbody>
</table>
</div>
);
}

const mapStateToProps = state => {
return {
users: state.users
};
};

const mapDispatchToProps = dispatch => {
return {
onDelete: id => {
dispatch(deleteUser(id));
},
onEdit: id => {
dispatch(updateUser(id))
}
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(UserList);


src/reducers/userReducer.js



import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';

export default function userReducer(state = , action) {
switch (action.type) {
case ADD_USER:
return [...state, action.payload];
case DELETE_USER:
return state.filter(user => user._id !== action.payload.id);
case UPDATE_USER:
return [...state, action.payload];
case FETCH_USER:
return action.users;
default:
return state;
}
}


Solution I thought but could not reproduce



I need to get the value of the id of the value table item that comes as key in the UserList, pass to the onClick parameter of the user component button, and pass the value of the id when I click edit in some table item to the Form in NewUser, in order to be able to edit the table item using onEdit in NewUser.





Stucked in solution ma_dev_15



I created a const initialState, with current user, but my userReducer State just looks the users,



src/recuders/userReducer.js
import { ADD_USER, DELETE_USER, UPDATE_USER, UPDATE_CURRENT_USER, FETCH_USER } from '../constants/ActionTypes';



const initialState = {
users: ,
currentUser: {},
}

export default function userReducer(state = initialState, action) {
switch (action.type) {
case ADD_USER:
return [...state, action.payload];
case DELETE_USER:
return state.filter(user => user._id !== action.payload.id);
case UPDATE_USER:
return updateObject(state, action)
case UPDATE_CURRENT_USER:
return [...state, action.currentUser];
case FETCH_USER:
return action.users;
default:
return state;
}
}

function updateObject(array, action) {

return array.map((item, index) => {
if (item._id !== action.payload._id) {
return item
}

return {
...item,
...action.payload
}
})
}


export reducers
src/reducers/index.js



import { combineReducers } from 'redux';
import users from './userReducer';
import currentUser from './userReducer';

export default combineReducers({
users: users,
currentUser: currentUser
});


user actions: src/actions/user.js



//Ommited
export const updateCurrentUserSuccess = (currentUser) => {
return {
type: UPDATE_CURRENT_USER,
currentUser
}
}

export const updateCurrentUser = (id) => {
return (dispatch) => {
return axios.get(`${apiUrl}/users/${id}`)
.then(response => {
dispatch(updateCurrentUserSuccess(response.data.data))
})
.catch(error => {
throw (error);
});
};
};
//Ommited


I make my UserList connected to CreateUser



src/components/containers/UserList



import React from 'react';
import { connect } from 'react-redux';
import User from '../components/User';
import { deleteUser, updateCurrentUser } from '../actions/user';
import NewUser from '../components/NewUser';

function UserList({ users, onDelete, onEditUser }) {
if (!users.length) {
return (
<div className="margin-top">
No Users
</div>
)
}
return (
<div className="margin-top">
<table className="table table-striped">
<thead>
<tr>
<th scope="col">Nome</th>
<th scope="col">CPF/CNPJ</th>
</tr>
</thead>
<tbody>
{users.map(user => {
return (
<User user={user} onDelete={onDelete} onEditUser={onEditUser} key={user._id} />
);
})}
</tbody>
</table>
</div>
);
}

const mapStateToProps = state => {
return {
users: state.users
};
};

const mapDispatchToProps = dispatch => {
return {
onDelete: id => {
dispatch(deleteUser(id));
},
onEditUser: (id) => {
dispatch(updateCurrentUser(id))
}
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(UserList, NewUser);


And when I click in Edit, and try to see console.log(Store.getState) in NewUser.js just returns me all users, I don't have currentUser.










share|improve this question





























    2















    I'm learning React-Redux and I'm stuck in a part of learning, I'm trying to edit the user value from table using the form that already exists, thereby taking the value of the line, and passing to the form, and also saving the user id in a variable or something of the type to update the user, I do not know how to proceed with that part



    enter image description here



    my actions: src/actions/user.js



    import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';
    import axios from 'axios';

    const apiUrl = 'http://localhost:8080/api/v1';

    export const createUser = ({ name, cpfcnpj }) => {
    return (dispatch) => {
    return axios.post(`${apiUrl}/users/`, { name, cpfcnpj })
    .then(response => {
    dispatch(createUserSuccess(response.data.data))
    })
    .catch(error => {
    throw (error);
    });
    };
    };

    export const createUserSuccess = (data) => {
    return {
    type: ADD_USER,
    payload: {
    _id: data._id,
    name: data.name,
    cpfcnpj: data.cpfcnpj
    }
    }
    };

    export const deleteUserSuccess = id => {
    return {
    type: DELETE_USER,
    payload: {
    id
    }
    }
    }

    export const deleteUser = (id) => {
    console.log(id)
    return (dispatch) => {
    return axios.delete(`${apiUrl}/users/${id}`)
    .then(response => {
    dispatch(deleteUserSuccess(id))
    })
    .catch(error => {
    throw (error);
    });
    };
    };

    export const updateUserSuccess = (data) => {
    return {
    type: UPDATE_USER,
    payload: {
    _id: data._id,
    name: data.name,
    cpfcnpj: data.cpfcnpj
    }
    }
    }

    export const updateUser = (id, name, cpfcnpj) => {
    console.log(id, name, cpfcnpj)
    return (dispatch) => {
    return axios.put(`${apiUrl}/users/${id}`, { name, cpfcnpj })
    .then(response => {
    dispatch(updateUserSuccess(response.data.data))
    })
    .catch(error => {
    throw (error);
    });
    };
    };

    export const fetchUsers = (users) => {
    return {
    type: FETCH_USER,
    users
    }
    };

    export const fetchAllUsers = () => {
    return (dispatch) => {
    return axios.get(`${apiUrl}/users/`)
    .then(response => {
    dispatch(fetchUsers(response.data.data))
    })
    .catch(error => {
    throw (error);
    });
    };
    };


    my Smart Component CreateUser: src/containers/CreateUser.js



    import { connect } from 'react-redux';
    import { createUser, updateUser } from '../actions/user';
    import NewUser from '../components/NewUser';

    const mapDispatchToProps = dispatch => {
    return {
    onAddUser: user => {
    dispatch(createUser(user));
    }
    };
    };

    export default connect(
    null,
    mapDispatchToProps
    )(NewUser);


    my Dummy Component NewUser: src/components/NewUser.js



    import React, { Component } from 'react';

    class NewUser extends Component {
    state = {
    name: '',
    cpfcnpj: '',
    isEdit: false
    };

    handleInputChange = e => {
    this.setState({
    [e.target.name]: e.target.value
    });
    };

    handleSubmit = e => {
    e.preventDefault();
    if (!this.state.isEdit) {
    if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
    this.props.onAddUser(this.state);
    this.handleReset();
    }
    } else {
    if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
    this.props.onEdit(this.state);
    this.handleReset();
    }
    }

    };

    handleReset = () => {
    this.setState({
    name: '',
    cpfcnpj: ''
    });
    };

    render() {
    return (
    <div>
    <form className="form-inline" onSubmit={this.handleSubmit}>
    <div className="form-group margin-right">
    <input
    type="text"
    placeholder="Name"
    className="form-control"
    name="name"
    onChange={this.handleInputChange}
    value={this.state.name}
    />
    </div>
    <div className="form-group margin-right">
    <input
    type="text"
    placeholder="CPF/CNPJ"
    className="form-control"
    name="cpfcnpj"
    onChange={this.handleInputChange}
    value={this.state.cpfcnpj}>
    </input>
    </div>
    <div className="form-group">
    <button type="submit" className={this.state.isEdit ? "btn btn-success margin-right hidden" : "btn btn-success margin-right"}>
    <span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
    &nbsp;
    Adicionar
    </button>
    <button type="submit" className={this.state.isEdit ? "btn btn-primary margin-right" : "btn btn-primary margin-right hidden"}>
    <span className="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>
    &nbsp;
    Salvar
    </button>
    <button type="button" className="btn btn-default margin-right" onClick={this.handleReset}>
    <span className="glyphicon glyphicon-erase" aria-hidden="true"></span>
    &nbsp;
    Limpar
    </button>
    </div>
    </form>
    </div>
    );
    }
    }

    export default NewUser;


    src/containers/UserList.js



    import React from 'react';
    import { connect } from 'react-redux';
    import User from '../components/User';
    import { deleteUser, updateUser } from '../actions/user';

    function UserList({ users, onDelete, onEdit }) {
    if (!users.length) {
    return (
    <div className="margin-top">
    No Users
    </div>
    )
    }
    return (
    <div className="margin-top">
    <table className="table table-striped">
    <thead>
    <tr>
    <th scope="col">Nome</th>
    <th scope="col">CPF/CNPJ</th>
    </tr>
    </thead>
    <tbody>
    {users.map(user => {
    return (
    <User user={user} onDelete={onDelete} key={user._id} onEdit={onEdit} />
    );
    })}
    </tbody>
    </table>
    </div>
    );
    }

    const mapStateToProps = state => {
    return {
    users: state.users
    };
    };

    const mapDispatchToProps = dispatch => {
    return {
    onDelete: id => {
    dispatch(deleteUser(id));
    },
    onEdit: id => {
    dispatch(updateUser(id))
    }
    };
    };

    export default connect(
    mapStateToProps,
    mapDispatchToProps
    )(UserList);


    src/reducers/userReducer.js



    import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';

    export default function userReducer(state = , action) {
    switch (action.type) {
    case ADD_USER:
    return [...state, action.payload];
    case DELETE_USER:
    return state.filter(user => user._id !== action.payload.id);
    case UPDATE_USER:
    return [...state, action.payload];
    case FETCH_USER:
    return action.users;
    default:
    return state;
    }
    }


    Solution I thought but could not reproduce



    I need to get the value of the id of the value table item that comes as key in the UserList, pass to the onClick parameter of the user component button, and pass the value of the id when I click edit in some table item to the Form in NewUser, in order to be able to edit the table item using onEdit in NewUser.





    Stucked in solution ma_dev_15



    I created a const initialState, with current user, but my userReducer State just looks the users,



    src/recuders/userReducer.js
    import { ADD_USER, DELETE_USER, UPDATE_USER, UPDATE_CURRENT_USER, FETCH_USER } from '../constants/ActionTypes';



    const initialState = {
    users: ,
    currentUser: {},
    }

    export default function userReducer(state = initialState, action) {
    switch (action.type) {
    case ADD_USER:
    return [...state, action.payload];
    case DELETE_USER:
    return state.filter(user => user._id !== action.payload.id);
    case UPDATE_USER:
    return updateObject(state, action)
    case UPDATE_CURRENT_USER:
    return [...state, action.currentUser];
    case FETCH_USER:
    return action.users;
    default:
    return state;
    }
    }

    function updateObject(array, action) {

    return array.map((item, index) => {
    if (item._id !== action.payload._id) {
    return item
    }

    return {
    ...item,
    ...action.payload
    }
    })
    }


    export reducers
    src/reducers/index.js



    import { combineReducers } from 'redux';
    import users from './userReducer';
    import currentUser from './userReducer';

    export default combineReducers({
    users: users,
    currentUser: currentUser
    });


    user actions: src/actions/user.js



    //Ommited
    export const updateCurrentUserSuccess = (currentUser) => {
    return {
    type: UPDATE_CURRENT_USER,
    currentUser
    }
    }

    export const updateCurrentUser = (id) => {
    return (dispatch) => {
    return axios.get(`${apiUrl}/users/${id}`)
    .then(response => {
    dispatch(updateCurrentUserSuccess(response.data.data))
    })
    .catch(error => {
    throw (error);
    });
    };
    };
    //Ommited


    I make my UserList connected to CreateUser



    src/components/containers/UserList



    import React from 'react';
    import { connect } from 'react-redux';
    import User from '../components/User';
    import { deleteUser, updateCurrentUser } from '../actions/user';
    import NewUser from '../components/NewUser';

    function UserList({ users, onDelete, onEditUser }) {
    if (!users.length) {
    return (
    <div className="margin-top">
    No Users
    </div>
    )
    }
    return (
    <div className="margin-top">
    <table className="table table-striped">
    <thead>
    <tr>
    <th scope="col">Nome</th>
    <th scope="col">CPF/CNPJ</th>
    </tr>
    </thead>
    <tbody>
    {users.map(user => {
    return (
    <User user={user} onDelete={onDelete} onEditUser={onEditUser} key={user._id} />
    );
    })}
    </tbody>
    </table>
    </div>
    );
    }

    const mapStateToProps = state => {
    return {
    users: state.users
    };
    };

    const mapDispatchToProps = dispatch => {
    return {
    onDelete: id => {
    dispatch(deleteUser(id));
    },
    onEditUser: (id) => {
    dispatch(updateCurrentUser(id))
    }
    };
    };

    export default connect(
    mapStateToProps,
    mapDispatchToProps
    )(UserList, NewUser);


    And when I click in Edit, and try to see console.log(Store.getState) in NewUser.js just returns me all users, I don't have currentUser.










    share|improve this question



























      2












      2








      2


      0






      I'm learning React-Redux and I'm stuck in a part of learning, I'm trying to edit the user value from table using the form that already exists, thereby taking the value of the line, and passing to the form, and also saving the user id in a variable or something of the type to update the user, I do not know how to proceed with that part



      enter image description here



      my actions: src/actions/user.js



      import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';
      import axios from 'axios';

      const apiUrl = 'http://localhost:8080/api/v1';

      export const createUser = ({ name, cpfcnpj }) => {
      return (dispatch) => {
      return axios.post(`${apiUrl}/users/`, { name, cpfcnpj })
      .then(response => {
      dispatch(createUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const createUserSuccess = (data) => {
      return {
      type: ADD_USER,
      payload: {
      _id: data._id,
      name: data.name,
      cpfcnpj: data.cpfcnpj
      }
      }
      };

      export const deleteUserSuccess = id => {
      return {
      type: DELETE_USER,
      payload: {
      id
      }
      }
      }

      export const deleteUser = (id) => {
      console.log(id)
      return (dispatch) => {
      return axios.delete(`${apiUrl}/users/${id}`)
      .then(response => {
      dispatch(deleteUserSuccess(id))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const updateUserSuccess = (data) => {
      return {
      type: UPDATE_USER,
      payload: {
      _id: data._id,
      name: data.name,
      cpfcnpj: data.cpfcnpj
      }
      }
      }

      export const updateUser = (id, name, cpfcnpj) => {
      console.log(id, name, cpfcnpj)
      return (dispatch) => {
      return axios.put(`${apiUrl}/users/${id}`, { name, cpfcnpj })
      .then(response => {
      dispatch(updateUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const fetchUsers = (users) => {
      return {
      type: FETCH_USER,
      users
      }
      };

      export const fetchAllUsers = () => {
      return (dispatch) => {
      return axios.get(`${apiUrl}/users/`)
      .then(response => {
      dispatch(fetchUsers(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };


      my Smart Component CreateUser: src/containers/CreateUser.js



      import { connect } from 'react-redux';
      import { createUser, updateUser } from '../actions/user';
      import NewUser from '../components/NewUser';

      const mapDispatchToProps = dispatch => {
      return {
      onAddUser: user => {
      dispatch(createUser(user));
      }
      };
      };

      export default connect(
      null,
      mapDispatchToProps
      )(NewUser);


      my Dummy Component NewUser: src/components/NewUser.js



      import React, { Component } from 'react';

      class NewUser extends Component {
      state = {
      name: '',
      cpfcnpj: '',
      isEdit: false
      };

      handleInputChange = e => {
      this.setState({
      [e.target.name]: e.target.value
      });
      };

      handleSubmit = e => {
      e.preventDefault();
      if (!this.state.isEdit) {
      if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
      this.props.onAddUser(this.state);
      this.handleReset();
      }
      } else {
      if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
      this.props.onEdit(this.state);
      this.handleReset();
      }
      }

      };

      handleReset = () => {
      this.setState({
      name: '',
      cpfcnpj: ''
      });
      };

      render() {
      return (
      <div>
      <form className="form-inline" onSubmit={this.handleSubmit}>
      <div className="form-group margin-right">
      <input
      type="text"
      placeholder="Name"
      className="form-control"
      name="name"
      onChange={this.handleInputChange}
      value={this.state.name}
      />
      </div>
      <div className="form-group margin-right">
      <input
      type="text"
      placeholder="CPF/CNPJ"
      className="form-control"
      name="cpfcnpj"
      onChange={this.handleInputChange}
      value={this.state.cpfcnpj}>
      </input>
      </div>
      <div className="form-group">
      <button type="submit" className={this.state.isEdit ? "btn btn-success margin-right hidden" : "btn btn-success margin-right"}>
      <span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
      &nbsp;
      Adicionar
      </button>
      <button type="submit" className={this.state.isEdit ? "btn btn-primary margin-right" : "btn btn-primary margin-right hidden"}>
      <span className="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>
      &nbsp;
      Salvar
      </button>
      <button type="button" className="btn btn-default margin-right" onClick={this.handleReset}>
      <span className="glyphicon glyphicon-erase" aria-hidden="true"></span>
      &nbsp;
      Limpar
      </button>
      </div>
      </form>
      </div>
      );
      }
      }

      export default NewUser;


      src/containers/UserList.js



      import React from 'react';
      import { connect } from 'react-redux';
      import User from '../components/User';
      import { deleteUser, updateUser } from '../actions/user';

      function UserList({ users, onDelete, onEdit }) {
      if (!users.length) {
      return (
      <div className="margin-top">
      No Users
      </div>
      )
      }
      return (
      <div className="margin-top">
      <table className="table table-striped">
      <thead>
      <tr>
      <th scope="col">Nome</th>
      <th scope="col">CPF/CNPJ</th>
      </tr>
      </thead>
      <tbody>
      {users.map(user => {
      return (
      <User user={user} onDelete={onDelete} key={user._id} onEdit={onEdit} />
      );
      })}
      </tbody>
      </table>
      </div>
      );
      }

      const mapStateToProps = state => {
      return {
      users: state.users
      };
      };

      const mapDispatchToProps = dispatch => {
      return {
      onDelete: id => {
      dispatch(deleteUser(id));
      },
      onEdit: id => {
      dispatch(updateUser(id))
      }
      };
      };

      export default connect(
      mapStateToProps,
      mapDispatchToProps
      )(UserList);


      src/reducers/userReducer.js



      import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';

      export default function userReducer(state = , action) {
      switch (action.type) {
      case ADD_USER:
      return [...state, action.payload];
      case DELETE_USER:
      return state.filter(user => user._id !== action.payload.id);
      case UPDATE_USER:
      return [...state, action.payload];
      case FETCH_USER:
      return action.users;
      default:
      return state;
      }
      }


      Solution I thought but could not reproduce



      I need to get the value of the id of the value table item that comes as key in the UserList, pass to the onClick parameter of the user component button, and pass the value of the id when I click edit in some table item to the Form in NewUser, in order to be able to edit the table item using onEdit in NewUser.





      Stucked in solution ma_dev_15



      I created a const initialState, with current user, but my userReducer State just looks the users,



      src/recuders/userReducer.js
      import { ADD_USER, DELETE_USER, UPDATE_USER, UPDATE_CURRENT_USER, FETCH_USER } from '../constants/ActionTypes';



      const initialState = {
      users: ,
      currentUser: {},
      }

      export default function userReducer(state = initialState, action) {
      switch (action.type) {
      case ADD_USER:
      return [...state, action.payload];
      case DELETE_USER:
      return state.filter(user => user._id !== action.payload.id);
      case UPDATE_USER:
      return updateObject(state, action)
      case UPDATE_CURRENT_USER:
      return [...state, action.currentUser];
      case FETCH_USER:
      return action.users;
      default:
      return state;
      }
      }

      function updateObject(array, action) {

      return array.map((item, index) => {
      if (item._id !== action.payload._id) {
      return item
      }

      return {
      ...item,
      ...action.payload
      }
      })
      }


      export reducers
      src/reducers/index.js



      import { combineReducers } from 'redux';
      import users from './userReducer';
      import currentUser from './userReducer';

      export default combineReducers({
      users: users,
      currentUser: currentUser
      });


      user actions: src/actions/user.js



      //Ommited
      export const updateCurrentUserSuccess = (currentUser) => {
      return {
      type: UPDATE_CURRENT_USER,
      currentUser
      }
      }

      export const updateCurrentUser = (id) => {
      return (dispatch) => {
      return axios.get(`${apiUrl}/users/${id}`)
      .then(response => {
      dispatch(updateCurrentUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };
      //Ommited


      I make my UserList connected to CreateUser



      src/components/containers/UserList



      import React from 'react';
      import { connect } from 'react-redux';
      import User from '../components/User';
      import { deleteUser, updateCurrentUser } from '../actions/user';
      import NewUser from '../components/NewUser';

      function UserList({ users, onDelete, onEditUser }) {
      if (!users.length) {
      return (
      <div className="margin-top">
      No Users
      </div>
      )
      }
      return (
      <div className="margin-top">
      <table className="table table-striped">
      <thead>
      <tr>
      <th scope="col">Nome</th>
      <th scope="col">CPF/CNPJ</th>
      </tr>
      </thead>
      <tbody>
      {users.map(user => {
      return (
      <User user={user} onDelete={onDelete} onEditUser={onEditUser} key={user._id} />
      );
      })}
      </tbody>
      </table>
      </div>
      );
      }

      const mapStateToProps = state => {
      return {
      users: state.users
      };
      };

      const mapDispatchToProps = dispatch => {
      return {
      onDelete: id => {
      dispatch(deleteUser(id));
      },
      onEditUser: (id) => {
      dispatch(updateCurrentUser(id))
      }
      };
      };

      export default connect(
      mapStateToProps,
      mapDispatchToProps
      )(UserList, NewUser);


      And when I click in Edit, and try to see console.log(Store.getState) in NewUser.js just returns me all users, I don't have currentUser.










      share|improve this question
















      I'm learning React-Redux and I'm stuck in a part of learning, I'm trying to edit the user value from table using the form that already exists, thereby taking the value of the line, and passing to the form, and also saving the user id in a variable or something of the type to update the user, I do not know how to proceed with that part



      enter image description here



      my actions: src/actions/user.js



      import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';
      import axios from 'axios';

      const apiUrl = 'http://localhost:8080/api/v1';

      export const createUser = ({ name, cpfcnpj }) => {
      return (dispatch) => {
      return axios.post(`${apiUrl}/users/`, { name, cpfcnpj })
      .then(response => {
      dispatch(createUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const createUserSuccess = (data) => {
      return {
      type: ADD_USER,
      payload: {
      _id: data._id,
      name: data.name,
      cpfcnpj: data.cpfcnpj
      }
      }
      };

      export const deleteUserSuccess = id => {
      return {
      type: DELETE_USER,
      payload: {
      id
      }
      }
      }

      export const deleteUser = (id) => {
      console.log(id)
      return (dispatch) => {
      return axios.delete(`${apiUrl}/users/${id}`)
      .then(response => {
      dispatch(deleteUserSuccess(id))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const updateUserSuccess = (data) => {
      return {
      type: UPDATE_USER,
      payload: {
      _id: data._id,
      name: data.name,
      cpfcnpj: data.cpfcnpj
      }
      }
      }

      export const updateUser = (id, name, cpfcnpj) => {
      console.log(id, name, cpfcnpj)
      return (dispatch) => {
      return axios.put(`${apiUrl}/users/${id}`, { name, cpfcnpj })
      .then(response => {
      dispatch(updateUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };

      export const fetchUsers = (users) => {
      return {
      type: FETCH_USER,
      users
      }
      };

      export const fetchAllUsers = () => {
      return (dispatch) => {
      return axios.get(`${apiUrl}/users/`)
      .then(response => {
      dispatch(fetchUsers(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };


      my Smart Component CreateUser: src/containers/CreateUser.js



      import { connect } from 'react-redux';
      import { createUser, updateUser } from '../actions/user';
      import NewUser from '../components/NewUser';

      const mapDispatchToProps = dispatch => {
      return {
      onAddUser: user => {
      dispatch(createUser(user));
      }
      };
      };

      export default connect(
      null,
      mapDispatchToProps
      )(NewUser);


      my Dummy Component NewUser: src/components/NewUser.js



      import React, { Component } from 'react';

      class NewUser extends Component {
      state = {
      name: '',
      cpfcnpj: '',
      isEdit: false
      };

      handleInputChange = e => {
      this.setState({
      [e.target.name]: e.target.value
      });
      };

      handleSubmit = e => {
      e.preventDefault();
      if (!this.state.isEdit) {
      if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
      this.props.onAddUser(this.state);
      this.handleReset();
      }
      } else {
      if (this.state.name.trim() && this.state.cpfcnpj.trim()) {
      this.props.onEdit(this.state);
      this.handleReset();
      }
      }

      };

      handleReset = () => {
      this.setState({
      name: '',
      cpfcnpj: ''
      });
      };

      render() {
      return (
      <div>
      <form className="form-inline" onSubmit={this.handleSubmit}>
      <div className="form-group margin-right">
      <input
      type="text"
      placeholder="Name"
      className="form-control"
      name="name"
      onChange={this.handleInputChange}
      value={this.state.name}
      />
      </div>
      <div className="form-group margin-right">
      <input
      type="text"
      placeholder="CPF/CNPJ"
      className="form-control"
      name="cpfcnpj"
      onChange={this.handleInputChange}
      value={this.state.cpfcnpj}>
      </input>
      </div>
      <div className="form-group">
      <button type="submit" className={this.state.isEdit ? "btn btn-success margin-right hidden" : "btn btn-success margin-right"}>
      <span className="glyphicon glyphicon-plus" aria-hidden="true"></span>
      &nbsp;
      Adicionar
      </button>
      <button type="submit" className={this.state.isEdit ? "btn btn-primary margin-right" : "btn btn-primary margin-right hidden"}>
      <span className="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>
      &nbsp;
      Salvar
      </button>
      <button type="button" className="btn btn-default margin-right" onClick={this.handleReset}>
      <span className="glyphicon glyphicon-erase" aria-hidden="true"></span>
      &nbsp;
      Limpar
      </button>
      </div>
      </form>
      </div>
      );
      }
      }

      export default NewUser;


      src/containers/UserList.js



      import React from 'react';
      import { connect } from 'react-redux';
      import User from '../components/User';
      import { deleteUser, updateUser } from '../actions/user';

      function UserList({ users, onDelete, onEdit }) {
      if (!users.length) {
      return (
      <div className="margin-top">
      No Users
      </div>
      )
      }
      return (
      <div className="margin-top">
      <table className="table table-striped">
      <thead>
      <tr>
      <th scope="col">Nome</th>
      <th scope="col">CPF/CNPJ</th>
      </tr>
      </thead>
      <tbody>
      {users.map(user => {
      return (
      <User user={user} onDelete={onDelete} key={user._id} onEdit={onEdit} />
      );
      })}
      </tbody>
      </table>
      </div>
      );
      }

      const mapStateToProps = state => {
      return {
      users: state.users
      };
      };

      const mapDispatchToProps = dispatch => {
      return {
      onDelete: id => {
      dispatch(deleteUser(id));
      },
      onEdit: id => {
      dispatch(updateUser(id))
      }
      };
      };

      export default connect(
      mapStateToProps,
      mapDispatchToProps
      )(UserList);


      src/reducers/userReducer.js



      import { ADD_USER, DELETE_USER, UPDATE_USER, FETCH_USER } from '../constants/ActionTypes';

      export default function userReducer(state = , action) {
      switch (action.type) {
      case ADD_USER:
      return [...state, action.payload];
      case DELETE_USER:
      return state.filter(user => user._id !== action.payload.id);
      case UPDATE_USER:
      return [...state, action.payload];
      case FETCH_USER:
      return action.users;
      default:
      return state;
      }
      }


      Solution I thought but could not reproduce



      I need to get the value of the id of the value table item that comes as key in the UserList, pass to the onClick parameter of the user component button, and pass the value of the id when I click edit in some table item to the Form in NewUser, in order to be able to edit the table item using onEdit in NewUser.





      Stucked in solution ma_dev_15



      I created a const initialState, with current user, but my userReducer State just looks the users,



      src/recuders/userReducer.js
      import { ADD_USER, DELETE_USER, UPDATE_USER, UPDATE_CURRENT_USER, FETCH_USER } from '../constants/ActionTypes';



      const initialState = {
      users: ,
      currentUser: {},
      }

      export default function userReducer(state = initialState, action) {
      switch (action.type) {
      case ADD_USER:
      return [...state, action.payload];
      case DELETE_USER:
      return state.filter(user => user._id !== action.payload.id);
      case UPDATE_USER:
      return updateObject(state, action)
      case UPDATE_CURRENT_USER:
      return [...state, action.currentUser];
      case FETCH_USER:
      return action.users;
      default:
      return state;
      }
      }

      function updateObject(array, action) {

      return array.map((item, index) => {
      if (item._id !== action.payload._id) {
      return item
      }

      return {
      ...item,
      ...action.payload
      }
      })
      }


      export reducers
      src/reducers/index.js



      import { combineReducers } from 'redux';
      import users from './userReducer';
      import currentUser from './userReducer';

      export default combineReducers({
      users: users,
      currentUser: currentUser
      });


      user actions: src/actions/user.js



      //Ommited
      export const updateCurrentUserSuccess = (currentUser) => {
      return {
      type: UPDATE_CURRENT_USER,
      currentUser
      }
      }

      export const updateCurrentUser = (id) => {
      return (dispatch) => {
      return axios.get(`${apiUrl}/users/${id}`)
      .then(response => {
      dispatch(updateCurrentUserSuccess(response.data.data))
      })
      .catch(error => {
      throw (error);
      });
      };
      };
      //Ommited


      I make my UserList connected to CreateUser



      src/components/containers/UserList



      import React from 'react';
      import { connect } from 'react-redux';
      import User from '../components/User';
      import { deleteUser, updateCurrentUser } from '../actions/user';
      import NewUser from '../components/NewUser';

      function UserList({ users, onDelete, onEditUser }) {
      if (!users.length) {
      return (
      <div className="margin-top">
      No Users
      </div>
      )
      }
      return (
      <div className="margin-top">
      <table className="table table-striped">
      <thead>
      <tr>
      <th scope="col">Nome</th>
      <th scope="col">CPF/CNPJ</th>
      </tr>
      </thead>
      <tbody>
      {users.map(user => {
      return (
      <User user={user} onDelete={onDelete} onEditUser={onEditUser} key={user._id} />
      );
      })}
      </tbody>
      </table>
      </div>
      );
      }

      const mapStateToProps = state => {
      return {
      users: state.users
      };
      };

      const mapDispatchToProps = dispatch => {
      return {
      onDelete: id => {
      dispatch(deleteUser(id));
      },
      onEditUser: (id) => {
      dispatch(updateCurrentUser(id))
      }
      };
      };

      export default connect(
      mapStateToProps,
      mapDispatchToProps
      )(UserList, NewUser);


      And when I click in Edit, and try to see console.log(Store.getState) in NewUser.js just returns me all users, I don't have currentUser.







      javascript reactjs redux react-redux






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 15:07







      Jefferson Bruchado

















      asked Jan 3 at 0:32









      Jefferson BruchadoJefferson Bruchado

      411414




      411414
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can create userReducer state like this:



          const initialState = {
          users: ,
          currentUser :{},
          }

          export default function userReducer(state = initialState, action) {
          switch (action.type) {
          case ADD_USER:
          return [...state, action.payload];
          case DELETE_USER:
          return state.filter(user => user._id !== action.payload.id);
          case UPDATE_USER:
          return [...state, action.payload];
          case FETCH_USER:
          return action.users;
          default:
          return state;
          }
          }


          After that let me tell you few simple steps:




          1. On clicking edit button pass userId and corres to that action update
            the currentUser in userReducer.

          2. Make the NewUser component connected component and get the currentUser from store and set to the input fields.

          3. On successfull edit update the currentUser as empty and update users list


          I hope you got what I mean. Let me know if you stuck somewhere.






          share|improve this answer
























          • Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

            – Jefferson Bruchado
            Jan 3 at 14:52











          • When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

            – Jefferson Bruchado
            Jan 3 at 14:56











          • Look my last edit

            – Jefferson Bruchado
            Jan 3 at 15:08











          • Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

            – ma_dev_15
            Jan 4 at 4:46











          • Not sure why you combined reducer?

            – ma_dev_15
            Jan 4 at 4:50












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014940%2fedit-table-values-using-react-redux%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can create userReducer state like this:



          const initialState = {
          users: ,
          currentUser :{},
          }

          export default function userReducer(state = initialState, action) {
          switch (action.type) {
          case ADD_USER:
          return [...state, action.payload];
          case DELETE_USER:
          return state.filter(user => user._id !== action.payload.id);
          case UPDATE_USER:
          return [...state, action.payload];
          case FETCH_USER:
          return action.users;
          default:
          return state;
          }
          }


          After that let me tell you few simple steps:




          1. On clicking edit button pass userId and corres to that action update
            the currentUser in userReducer.

          2. Make the NewUser component connected component and get the currentUser from store and set to the input fields.

          3. On successfull edit update the currentUser as empty and update users list


          I hope you got what I mean. Let me know if you stuck somewhere.






          share|improve this answer
























          • Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

            – Jefferson Bruchado
            Jan 3 at 14:52











          • When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

            – Jefferson Bruchado
            Jan 3 at 14:56











          • Look my last edit

            – Jefferson Bruchado
            Jan 3 at 15:08











          • Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

            – ma_dev_15
            Jan 4 at 4:46











          • Not sure why you combined reducer?

            – ma_dev_15
            Jan 4 at 4:50
















          1














          You can create userReducer state like this:



          const initialState = {
          users: ,
          currentUser :{},
          }

          export default function userReducer(state = initialState, action) {
          switch (action.type) {
          case ADD_USER:
          return [...state, action.payload];
          case DELETE_USER:
          return state.filter(user => user._id !== action.payload.id);
          case UPDATE_USER:
          return [...state, action.payload];
          case FETCH_USER:
          return action.users;
          default:
          return state;
          }
          }


          After that let me tell you few simple steps:




          1. On clicking edit button pass userId and corres to that action update
            the currentUser in userReducer.

          2. Make the NewUser component connected component and get the currentUser from store and set to the input fields.

          3. On successfull edit update the currentUser as empty and update users list


          I hope you got what I mean. Let me know if you stuck somewhere.






          share|improve this answer
























          • Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

            – Jefferson Bruchado
            Jan 3 at 14:52











          • When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

            – Jefferson Bruchado
            Jan 3 at 14:56











          • Look my last edit

            – Jefferson Bruchado
            Jan 3 at 15:08











          • Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

            – ma_dev_15
            Jan 4 at 4:46











          • Not sure why you combined reducer?

            – ma_dev_15
            Jan 4 at 4:50














          1












          1








          1







          You can create userReducer state like this:



          const initialState = {
          users: ,
          currentUser :{},
          }

          export default function userReducer(state = initialState, action) {
          switch (action.type) {
          case ADD_USER:
          return [...state, action.payload];
          case DELETE_USER:
          return state.filter(user => user._id !== action.payload.id);
          case UPDATE_USER:
          return [...state, action.payload];
          case FETCH_USER:
          return action.users;
          default:
          return state;
          }
          }


          After that let me tell you few simple steps:




          1. On clicking edit button pass userId and corres to that action update
            the currentUser in userReducer.

          2. Make the NewUser component connected component and get the currentUser from store and set to the input fields.

          3. On successfull edit update the currentUser as empty and update users list


          I hope you got what I mean. Let me know if you stuck somewhere.






          share|improve this answer













          You can create userReducer state like this:



          const initialState = {
          users: ,
          currentUser :{},
          }

          export default function userReducer(state = initialState, action) {
          switch (action.type) {
          case ADD_USER:
          return [...state, action.payload];
          case DELETE_USER:
          return state.filter(user => user._id !== action.payload.id);
          case UPDATE_USER:
          return [...state, action.payload];
          case FETCH_USER:
          return action.users;
          default:
          return state;
          }
          }


          After that let me tell you few simple steps:




          1. On clicking edit button pass userId and corres to that action update
            the currentUser in userReducer.

          2. Make the NewUser component connected component and get the currentUser from store and set to the input fields.

          3. On successfull edit update the currentUser as empty and update users list


          I hope you got what I mean. Let me know if you stuck somewhere.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 6:43









          ma_dev_15ma_dev_15

          407212




          407212













          • Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

            – Jefferson Bruchado
            Jan 3 at 14:52











          • When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

            – Jefferson Bruchado
            Jan 3 at 14:56











          • Look my last edit

            – Jefferson Bruchado
            Jan 3 at 15:08











          • Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

            – ma_dev_15
            Jan 4 at 4:46











          • Not sure why you combined reducer?

            – ma_dev_15
            Jan 4 at 4:50



















          • Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

            – Jefferson Bruchado
            Jan 3 at 14:52











          • When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

            – Jefferson Bruchado
            Jan 3 at 14:56











          • Look my last edit

            – Jefferson Bruchado
            Jan 3 at 15:08











          • Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

            – ma_dev_15
            Jan 4 at 4:46











          • Not sure why you combined reducer?

            – ma_dev_15
            Jan 4 at 4:50

















          Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

          – Jefferson Bruchado
          Jan 3 at 14:52





          Hi, thanks for the reply, I'm stuck in a part, you know how I can update currentUser state in userReducer? I did that way but doesn't worked: const initialState = { users: , currentUser: {}, } export default function userReducer(state = initialState, action) { switch (action.type) { //Ommitedz case UPDATE_CURRENT_USER: return [...state, action.currentUser]; //Ommited } }

          – Jefferson Bruchado
          Jan 3 at 14:52













          When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

          – Jefferson Bruchado
          Jan 3 at 14:56





          When I do, return [...state, action.currentUser]; or [...state, action.currentUser] the application returns me all users, and no just the currentUser

          – Jefferson Bruchado
          Jan 3 at 14:56













          Look my last edit

          – Jefferson Bruchado
          Jan 3 at 15:08





          Look my last edit

          – Jefferson Bruchado
          Jan 3 at 15:08













          Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

          – ma_dev_15
          Jan 4 at 4:46





          Yes you need to fire an action from component on clicking edit button, and then pass user id and copy the user details to currentUser and map currentUser in NewUser component

          – ma_dev_15
          Jan 4 at 4:46













          Not sure why you combined reducer?

          – ma_dev_15
          Jan 4 at 4:50





          Not sure why you combined reducer?

          – ma_dev_15
          Jan 4 at 4:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014940%2fedit-table-values-using-react-redux%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory