Я хочу установить 'username' в состоянии в localstorage.getItem, но проблема в том, что он не работает, есть предложения?
class ToDoApp extends Component {
state = {
username:'',
inputValue: '',
todos: [],
currentPage: 1,
pageCount: 1,
itemsPerPage: 10,
};
Функция с отправкой элемента данных: *
addItem = () => {
let {todos} = this.state
let userName = localStorage.getItem('username')
console.log(userName)
if (this.inpRef.current.value === '') {
return alert('We dont do that here....')
} else {
axios
.post(`http://localhost:8080/add`, {
username:userName,
todo: this.inpRef.current.value,
checked: false,
})
.then((res) => {
this.setState({
todos:[...todos,{username:res.data.username,todo:res.data.todo,_id:res.data._id,checked:false}]
})
console.log(todos)
})
.catch((err) => {
console.log("err", err);
});
this.setPageCount()
}
this.inpRef.current.value = ''
console.log('--------this.state.todos', this.state.todos);
}