Redux不更新状态

v8wbuo2f  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(114)

App.js:

function App() {
  return (
    <main>
      <AddPostForm />
      <PostList />
    </main>
  );
}

字符串
store.js:

export const store = configureStore({
  reducer: {
    posts: postsReducer
  }
})


postSlice.js:

const initialState = [
  { id: 1, title: 'Learning Redux Toolkit', content: 'I have heard good things.' },
  { id: 2, title: 'Slices...', content: 'The more I say slice, the more I want pizza.' }
];

const postsSlice = createSlice({
  name: 'posts',
  initialState,
  reducers: {
    postAdded: (state, action) => {
      console.log(action.payload)
      state.push(action.payload);
    }
  }
});

export const selectAllPosts = state => state.posts;

export const { postAdded } = postsSlice.actions;

export default postsSlice.reducer;


AddPostForm.js:

function AddPostForm() {
  const dispatch = useDispatch();
  const [title, setTitle] = useState('');
  const [content, setContent] = useState('');

  function onSubmit() {
    if (title && content) {
      console.log('after if');
      dispatch(postAdded({
        id: nanoid(),
        title,
        content
      }));
      setTitle('');
      setContent('');
    }
  }

  return (
    <section onSubmit={onSubmit}>
      <h2>Add New Post</h2>
      <form>
        <label htmlFor="title">Post Title:</label>
        <input
          type="text"
          id="title"
          value={title}
          onChange={e => setTitle(e.target.value)}
        />
        <label htmlFor="content">Content:</label>
        <textarea
          id="content"
          value={content}
          onChange={e => setContent(e.target.value)}
        />
        <button>Save Post</button>
      </form>
    </section>
  )
}

export default AddPostForm


PostList.js:

function PostList() {
  const posts = useSelector(selectAllPosts);

  return (
    <section>
      <h2>Posts</h2>
      {posts.map(post => (
        <article key={post.id}>
          <h3>{post.title}</h3>
          <p>{post.content.substring(0, 100)}</p>
        </article>
      ))}
    </section>
  )
}

export default PostList


postAdded reducer是push ing到状态,但在我添加一个测试帖子后(通过表单),我没有看到添加的表单。然而console.log(action.payload)确实显示了对象(例如Object { id: "rTuWGH9pfK-cQvCZecSvt", title: "asd", content: "asd" })。那么为什么它没有将其推入状态?

bqf10yzr

bqf10yzr1#

如果未指定,button元素默认为type="submit"。这里的问题是,form元素被提交,并且默认表单操作未被阻止。结果是页面重新加载,例如React应用被重新挂载,所有状态都是初始状态。
onSubmit处理程序移动到form元素,并在onSubmit事件对象上调用preventDefault。此外,最好显式地使用属性,将type="submit"添加到button元素。

function AddPostForm() {
  const dispatch = useDispatch();

  const [title, setTitle] = useState('');
  const [content, setContent] = useState('');

  function onSubmit(event) {
    event.preventDefault(); // <-- prevent form submission, don't reload page

    if (title && content) {
      dispatch(postAdded({ title, content }));
      setTitle('');
      setContent('');
    }
  }

  return (
    <section>
      <h2>Add New Post</h2>
      <form onSubmit={onSubmit}> // <-- onSubmit on form element
        <label htmlFor="title">Post Title:</label>
        <input
          type="text"
          id="title"
          value={title}
          onChange={e => setTitle(e.target.value)}
        />
        <label htmlFor="content">Content:</label>
        <textarea
          id="content"
          value={content}
          onChange={e => setContent(e.target.value)}
        />
        <button type="submit"> // <-- explicit button type
          Save Post
        </button>
      </form>
    </section>
  );
}

个字符

相关问题