P
P
Pogran2016-09-28 13:14:48
React
Pogran, 2016-09-28 13:14:48

How to return the value of the added element?

I have such a structure.
event in view

onAddLevelNode = e => {
    e.preventDefault();
    const { addLevelNode, node : {id} } = this.props;
    addLevelNode(id, '', '');
  };

action
export const addLevelNode = (id, key, value) => {

  return {
    type: ADD_LEVEL_NODE,
    id,
    key,
    value
  }
};

reducer (it is not written according to the rules now, but that's not the point)
case ADD_LEVEL_NODE:
      const addLevelNode = structure => {
        const { id, value, key } = action;
        const nodeId = Object.keys(structure).length;
        structure[nodeId] = {
          id: nodeId,
          key: key,
          value: value,
          type: 'string',
          childIds: []
        };

        structure[id].value = false;
        structure[id].type = 'object';
        structure[id].childIds.push(nodeId);

        return structure;
      };

      return {
        ...state,
        structure: addLevelNode(state.structure)
      };

An empty element is added to the reducer, how can I get the id of the added element in the onAddLevelNode event?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2017-05-15
@KnightForce

To get an element - use ref : This is how you get it - then do what you want.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question