【问题标题】:Render child component after data add to state数据添加到状态后渲染子组件
【发布时间】:2020-12-24 16:36:16
【问题描述】:

我是 React 新手,我正在开发一个 React Redux 应用程序。当我将新项目添加到状态时,我想呈现显示项目列表的子组件。当前项目列表显示在状态中,新项目也添加到该列表中 (mobileList)。将新项目添加到状态时如何渲染子组件?当前子组件不显示任何内容。

父组件

import React, { useEffect, useState ,ComponentState} from "react";
import { useSelector, useDispatch } from "react-redux";
import { Button, Container, Row, Col, Form } from "react-bootstrap";
import uuid from "react-uuid";
import allActions from '../Actions/index'
import MobileList from './MobileList';

const MobileCreation = () => {
  const dispatch = useDispatch();

  const initialMobileState = {
    id: "",
    modelName: "",
    brand: "",
    year: "",
    price: "",
  };

  const [mobile, setMobile] = useState(initialMobileState);
  const [submitted, setSubmitted] = useState(false);
  const [submitSuccesful, setSubmitSuccesful] = useState(false);

  const handleInputChange = (event) => {
    const { name, value } = event.target;
    setMobile({ ...mobile, [name]: value });
  };

  const saveMobile = (event) => {
    event.preventDefault();
    setSubmitted(true);

    if (mobile.modelName && mobile.brand && mobile.year && mobile.price) {
      var data = {
        Id: uuid(),
        ModelName: mobile.modelName,
        Brand: mobile.brand,
        Year: mobile.year,
        Price: mobile.price,
      };
    }

   dispatch(allActions.MobileActions.save(data));
   setSubmitted(false);
   setMobile(initialMobileState);
  };

  return (
      <>
    <Form onSubmit={saveMobile}>
      <Form.Group controlId="mobile.ModelName">
        <Form.Label>Model Name</Form.Label>
        <Form.Control
          type="text"
          placeholder="Enter model name"
          name="modelName"
          value={mobile.modelName}
          onChange={handleInputChange}
        />
        {submitted && !mobile.modelName && <div>Model name is required</div>}
      </Form.Group>
      <Form.Group controlId="mobile.Brand">
        <Form.Label>Brand Name</Form.Label>
        <Form.Control
          type="text"
          placeholder="Enter Brand name"
          name="brand"
          value={mobile.brand}
          onChange={handleInputChange}
        />
        {submitted && !mobile.brand && <div>Brand name is required</div>}
      </Form.Group>
      <Form.Group controlId="mobile.Year">
        <Form.Label>Year</Form.Label>
        <Form.Control
          type="text"
          placeholder="Enter year"
          name="year"
          value={mobile.year}
          onChange={handleInputChange}
        />
        {submitted && !mobile.year && <div>Year is required</div>}
      </Form.Group>
      <Form.Group controlId="mobile.Price">
        <Form.Label>Price</Form.Label>
        <Form.Control
          type="text"
          placeholder="Enter price"
          name="price"
          value={mobile.price}
          onChange={handleInputChange}
        />
        {submitted && !mobile.price && <div>Price is required</div>}
      </Form.Group>
      <Button variant="primary" type="submit">
        Submit
      </Button>
    </Form>
    <MobileList/>
    </>
  );
};

export default MobileCreation;

子组件

import React, { useEffect } from "react";
import { useSelector, useDispatch ,} from "react-redux";
import Table from 'react-bootstrap/Table'

const MobileList = () => {
  const mobileList = useSelector((state) => state.mobileList);

  return (
    <>
      <Table striped bordered hover size="sm">
        <thead>
          <tr>
            <th>Model Name</th>
            <th>Brand Name</th>
            <th>Year</th>
            <th>Price</th>
          </tr>
        </thead>
        <tbody>{mobileList ? 
             mobileList.map(function(item, i){
            <tr>
                <td>item.ModelName</td>
                <td>item.Brand</td>
                <td>item.Year</td>
                <td>item.Price</td>
            </tr>
            }) :
           <tr>
               <td>No data</td>
           </tr>
        
            }
        </tbody>
      </Table>
    </>
  );
};

export default MobileList;

减速器

const initialMobileListState = {
    mobileList:[]
}


const mobileReducer= (state = initialMobileListState, action) => {
    switch(action.type){
        case "SAVE":
            return {
                ...state,
                mobileList:[...state.mobileList, action.mobile]
            }
        case "FAIL":
            return state 
        default: 
            return state
    }
}

export default mobileReducer

【问题讨论】:

  • 如我所见,您的子组件无论如何都会呈现。当 state.mobileList 更新时,它会自动更新。似乎问题在于如何更新状态。它包括“allActions.MobileActions.save”的工作原理以及如何更新减速器中的值。
  • 我已经用reducer代码更新了问题
  • 我看到你正确更新了reducer。那么问题是检查当您提交表单时它是否真的更新了?在那里放一些调试器。还有一个问题,state.mobileList 是否真的是检索数据的正确路径。
  • 好的,您检查该路径是否正确?是不是很像 state.MobileReducer.mobileList?如果没有,那么我需要一个 JS fiddle 来查看所有代码。
  • 感谢 Eugene 的帮助,问题在 state.mobileList 更改为 state.MobileReducer.mobileList 后排序

标签: reactjs redux react-redux


【解决方案1】:

问题出在选择器中的错误值。 你需要使用

state.MobileReducer.mobileList

而不是

state.mobileList

【讨论】:

    猜你喜欢
    • 2022-06-30
    • 2019-09-21
    • 2019-09-16
    • 1970-01-01
    • 2020-04-07
    • 2021-03-07
    • 2019-04-29
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多