1
let store = {
   _subscriber() {
      console.log("no subscribers")
   },
   _state: {
      profilePage: {
         posts: [
            { id: 1, message: "It's my first post!", likesCount: 22 },
            { id: 2, message: "How are you?", likesCount: 15 },
            { id: 3, message: "My name is Andrei!", likesCount: 17 }
         ],
         newPostText: "test text"
      },
      dialogsPage: {
         dialogs: [
            { id: 1, name: "Andrei" },
            { id: 2, name: "Evgeniy" },
            { id: 3, name: "Artrem" }
         ],
         messages: [
            { id: 1, message: "Hi" },
            { id: 2, message: "How are you?" },
            { id: 3, message: "Yo" }
         ]
      },
   },
   getState() {
      return this._state;
   },
   addPost() {
      let newPost = {
         id: 4,
         message: this._state.profilePage.newPostText,
         likesCount: 0
      }

      this._state.profilePage.posts.push(newPost);
      this._state.profilePage.newPostText = "";
      this._subscriber();
   },
   updateNewPostText(newText){
      this._state.profilePage.newPostText = newText;
      this._subscriber();
   },
   subscribe(observer){
      this._subscriber = observer;
   }
}

При выполнении addPost и updateNewPostText появляется ошибка, будто profilePage вообще в моём _state нету. В чем может быть проблема? P.S. Они вызываются, когда пытаюсь ввести текст в textarea или пытаюсь добавить пост.

2
  • 1
    Пожалуйста, уточните вашу конкретную проблему или приведите более подробную информацию о том, что именно вам нужно. В текущем виде сложно понять, что именно вы спрашиваете. 23 окт 2021 в 14:45
  • Скопировал код в консоль, запустил store.addPost(); вывело "no subscribers". updateNewPostText - аналогично.
    – Zombotron
    23 окт 2021 в 14:58

1 ответ 1

0

Зайди в main.js(или в index.js) и напиши bind , как здесь

let reRenderEntireTree = (state) => {
  ReactDOM.createRoot(document.getElementById("root")).render(
    <React.StrictMode>
      <App
        state={state}
        addPost={store.addPost.bind(store)}
        updateNewPostText={store.updateNewPostText.bind(store)}
      />
    </React.StrictMode>
  );
};

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.