【问题标题】:Warning: Failed prop type: The prop `todos[0].title` is marked as required in `TodoList`, but its value is `undefined`警告:失败的道具类型:道具`todos[0].title`在`TodoList`中标记为必需,但其值为`undefined`
【发布时间】:2018-03-12 06:09:06
【问题描述】:

我想为我的服务器添加标题,如您在图片中看到的 enter image description here

它的值可以,但它不能与标题一起使用,标题在我的数组中,你可以在图片中看到,但它仍然会出现这个错误,道具是在其他 JS 文件中定义的,这里有任何帮助

import React from 'react';
import todoInputProps from './TodoInput.props';

const TodoInput = (props) => {
  let input;
  const handleClick = () => {
    props.onAdd(input.value, input.title);
    input.title = '';
    input.value = '';

    input.focus();
  };

  return (
    <div>
      <input
        type="text"
        ref={(currentElement) => { input = currentElement; }}
        placeholder="title"
      />

      <input
        type="text"
        ref={(currentElement) => { input = currentElement; }}
        placeholder="value"
      />

      <button
        type="button"
        onClick={handleClick}
      >
      add item
      </button>
    </div>
  );
};

TodoInput.propTypes = todoInputProps;

export default TodoInput;

import React from 'react';

import todoItemProps from './TodoItem.props';
import './TodoItem.css';

const TodoItem = (props) => {
  const remove = () => {
    props.onRemove(props.id);
  };

  const animateClass = props.animate ? 'added-item' : '';
  return (
    <li className={`TodoItem-item ${animateClass}`}>
      <div className="TodoItem-value">{props.value}</div>
      <div className="TodoItem-title">{props.title}</div>
      <button
        onClick={remove}
      >
      X
      </button>
    </li>
  );
};

TodoItem.propTypes = todoItemProps;

export default TodoItem;

我的待办事项清单

import React from 'react';

import TodoItem from './components/TodoItem';
import todoListProps from './TodoList.props';
import './TodoList.css';

class TodoList extends React.Component {
  constructor() {
    super();
    this.state = {};
    this.handleRemove = this.handleRemove.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    nextProps.todos.forEach((todo) => {
      const oldValue = this.props.todos.find(oldTodo => oldTodo.id === todo.id);
      const isNewTodo = typeof oldValue === 'undefined';
      if (isNewTodo) {
        this.setState({ addedId: todo.id });
      }
    });
  }

  handleRemove(id) {
    this.props.onItemRemove(id);
  }

  render() {
    return (
      <ul className="TodoList">
        {
          this.props.todos.map(todoItem => (
            <TodoItem
              animate
              key={todoItem.id}
              id={todoItem.id}
              value={todoItem.value}
              title={todoItem.title}
              onRemove={this.handleRemove}
            />
          ))
        }
      </ul>
    );
  }
}

TodoList.propTypes = todoListProps;

export default TodoList;

我的容器看起来像那样

import React from 'react';

import TodoInput from './components/TodoInput';
import TodoList from './components/TodoList';
import { getAll, add, remove } from '../../../utils/todo';
import './TodoContainer.css';

class TodoContainer extends React.Component {
  constructor() {
    super();
    this.state = {
      todos: [],
    };
    this.handleRemove = this.handleRemove.bind(this);
    this.handleAdd = this.handleAdd.bind(this);
  }

  componentDidMount() {
    getAll().then((todos) => {
      this.setState({ todos });
    });
  }

  handleAdd(value, title) {
    add(value, title).then((id) => {
      this.setState({
        todos: this.state.todos.concat([{
          id,
          value,
          title,
        }]),
      });
    });
  }

  handleRemove(id) {
    remove(id).then(() => {
      this.setState({
        todos: this.state.todos.filter(todoItem => todoItem.id !== id),
      });
    });
  }

  render() {
    return (
      <div className="TodoContainer-wrapper">
        <TodoInput onAdd={this.handleAdd} />
        <TodoList
          todos={this.state.todos}
          onItemRemove={this.handleRemove}
        />
      </div>
    );
  }
}

export default TodoContainer;

【问题讨论】:

    标签: javascript java node.js reactjs


    【解决方案1】:

    问题是在TodoInput 中,您尝试将一个变量input 用于两个React 实例。这是TodoInput的更正代码:

    const TodoInput = (props) => {
      let inputTitle, inputValue;
      const handleClick = () => {
        props.onAdd(inputTitle.value, inputValue.value);
        inputTitle.value = '';
        inputValue.value = '';
    
        input.focus();
      };
    
      return (
        <div>
          <input
            type="text"
            ref={(currentElement) => { inputTitle = currentElement; }}
            placeholder="title"
          />
    
          <input
            type="text"
            ref={(currentElement) => { inputValue = currentElement; }}
            placeholder="value"
          />
    
          <button
            type="button"
            onClick={handleClick}
          >
          add item
          </button>
        </div>
      );
    };
    

    【讨论】:

    • 谢谢你的回答,它只是在前面工作,但在我的本地存储后端服务中,它不能用值和 id 保存倾斜
    • @user8054244 我认为最好提出一个新问题,没有input 错误并详细说明您的新问题。
    • 它的代码相同,但我使用 Axios 连接到我的本地存储,我在 Axios 中的代码看起来像 'const add = (title, value) => axios({ method: 'POST', url :${SERVICE_URL}/v1/items,数据:{标题,值},}).then(validateStatus(201)).then(response => response.data.id);'
    猜你喜欢
    • 2020-12-24
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2020-01-26
    • 2021-09-28
    相关资源
    最近更新 更多