Skip to content

Commit

Permalink
add generate id
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnAErmak committed Sep 10, 2024
1 parent af8f709 commit 5ac300e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { createElement } from './utils.js';
import {createElement, generateId} from './utils.js';
import App from './app.js';
import Store from './store.js';

const store = new Store({
list: [
{ code: 1, title: 'Название элемента' },
{ code: 2, title: 'Некий объект' },
{ code: 3, title: 'Заголовок' },
{ code: 4, title: 'Очень длинное название элемента из семи слов' },
{ code: 5, title: 'Запись' },
{ code: 6, title: 'Шестая запись' },
{ code: 7, title: 'Седьмая запись' },
{ code: generateId(), title: 'Название элемента' },
{ code: generateId(), title: 'Некий объект' },
{ code: generateId(), title: 'Заголовок' },
{ code: generateId(), title: 'Очень длинное название элемента из семи слов' },
{ code: generateId(), title: 'Запись' },
{ code: generateId(), title: 'Шестая запись' },
{ code: generateId(), title: 'Седьмая запись' },
],
});

Expand Down
4 changes: 3 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Хранилище состояния приложения
*/
import {generateId} from "./utils";

class Store {
constructor(initState = {}) {
this.state = initState;
Expand Down Expand Up @@ -44,7 +46,7 @@ class Store {
addItem() {
this.setState({
...this.state,
list: [...this.state.list, { code: this.state.list.length + 1, title: 'Новая запись' }],
list: [...this.state.list, { code: generateId(), title: 'Новая запись' }],
});
}

Expand Down
8 changes: 6 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const propNames = new Set(['id', 'className', 'textContent', 'onclick']);

let id = 1;
/**
* Создание элемента со свойствами и вложенными элементами
* @param name {String} Название HTML тега
Expand Down Expand Up @@ -34,4 +34,8 @@ export function getWordForCount(count) {
} else {
return 'раз';
}
}
};

export const generateId = () =>{
return id++;
};

0 comments on commit 5ac300e

Please sign in to comment.