简体   繁体   English

反应 map,道具未定义

[英]React map, props undefined

Here is my App.js code这是我的 App.js 代码

import React, { useState } from "react";
import { Route } from "react-router-dom";
import Home from "./Home/Home";
import TextsList from "./Text/TextsList";
import WriteButton from "./WriteButton/WriteButton";
import ListButton from "./ListButton/ListButton";
import WriteForm from "./WriteForm/WriteForm";
import "./App.css";

function App() {
  const [texts, setTexts] = useState([
    {
      id: 1,
      author: "Penguin",
      text: "Hello World!!",
    },
    {
      id: 2,
      author: "DonaldDuck",
      text: "Let's Play~~!!",
    },
    {
      id: 3,
      author: "Tom and Jerry",
      text: "Jerry Cheese Cake",
    },
  ]);
  const [nextId, setNextId] = useState(4);
  const [inputAuthor, setInputAuthor] = useState("");
  const [inputText, setInputText] = useState("");

  const onAuthorChange = (e) => {
    e.preventDefault();
    setInputAuthor(e.target.value);
  };

  const onTextChange = (e) => {
    e.preventDefault();
    setInputText(e.target.value);
  };

  const onClick = (e) => {
    e.preventDefault();
    const newTexts = texts.concat({
      id: nextId,
      author: inputAuthor,
      text: inputText,
    });

    setNextId(nextId + 1);
    setTexts(newTexts);
    setInputAuthor("");
    setInputText("");
    

  const textsList = texts.map((t) => <li key={t.id}>{t}</li>);

  return (
    <div className="all">
      <Route path="/" component={Home} />
      <div>
        <Route
          exact
          path="/"
          render={() => <TextsList textsList={textsList} />}
        />
        
      </div>
      <div id="WriteButton">
        <Route exact path="/" component={WriteButton} />
      </div>
      <div id="WriteForm">
        <div id="ListButton">
          <Route path="/writeform" component={ListButton} />
        </div>
        <Route
          path="/writeform"
          render={() => (
            <WriteForm
              inputAuthor={inputAuthor}
              onAuthorChange={onAuthorChange}
              inputText={inputText}
              onTextChange={onTextChange}
              onClick={onClick}
            />
          )}
        />
      </div>
    </div>
  );
}

export default App;


Here is my TextsList.jsx code这是我的 TextsList.jsx 代码

import React from "react";
import "./TextsList.css";

const TextList = ({ textsList }) => {
  console.log(textsList[2].props.children.text);
  return (
    <div id="TextListContainer">
      <p>John K - Parachute</p>
      <ol>{textsList.id}</ol>
    </div>
  );
};

export default TextList;

console.log(textsList[2].props.children.text); console.log(textsList[2].props.children.text); result => Jerry Cheese Cake结果 => 杰瑞芝士蛋糕

I want to make my TextsList.jsx like我想让我的 TextsList.jsx 像

return <div id="TextListContainer">
      <ol>{textsList.id}</ol>
    </div>

if I write the code like如果我写这样的代码

    {textsList.id} {textsList.id}
error pops up... 错误弹出...

I want to make all the texts render.我想让所有的文本渲染。

plz help me (My English is not good, so please forgive me)请帮助我(我的英语不好,所以请原谅我)

I am still a rookie, but this is what I did on my last react project using OOP.我仍然是一个菜鸟,但这是我在上一个使用 OOP 的反应项目中所做的。

If there is an error, I logged every step, one by one to see if the object was still following like I wanted it to.如果出现错误,我会一一记录下每个步骤,以查看 object 是否仍像我想要的那样跟随。

import React, { useState } from "react";
import "./TextsList.css";

const TextList = ({ textsList }) => {
  const [display, setDisplay] = useState();
  console.log(textsList[2].props.children.text);

// you can customize inside the map what info you want displayed
  setDisplay(
    <>
    {textList.map((text, index) => {
      return (
        <>
          <li key={index}>{text.path.to.prop}</li>
        </>
      );
    })};
    </>
  };


  return (
    <div id="TextListContainer">
      <ol>{display}</ol>
    </div>
  );
};

export default TextList;

I hope this works for you!我希望这对你有用!

No worries... we all struggle if English when we are not natives... I'm not sure if this is exactly what you are looking for... but to my understanding to do what you want you just need to make some tweeks to your code...不用担心...如果我们不是本地人,我们都会在英语中挣扎...我不确定这是否正是您正在寻找的...但据我了解,要做您想做的事,您只需要做一些tweeks 到您的代码...

First... user props.children on your TextList container... you could also use it as a props as you have done... but using props.children is cleaner and more intuitive when actually using the container... so...首先...在您的 TextList 容器上使用用户props.children ...您也可以像以前一样将其用作道具...但是在实际使用容器时使用props.children更清洁、更直观...所以。 ..

import React from "react";
import "./TextsList.css";

const TextList = (props) => {
  return (
    <div id="TextListContainer">
      <ol>{props.children}</ol>
    </div>
  );
};

export default TextList;

Second... add your TextList component to the render of your App.js using textList as its child.其次...将 TextList 组件添加到 App.js 的渲染中,使用textList作为子组件。

import TextList from './TextList';
const [texts, setTexts] = useState([
    {
      id: 1,
      author: "Penguin",
      text: "Hello World!!",
    },
    {
      id: 2,
      author: "DonaldDuck",
      text: "Let's Play~~!!",
    },
    {
      id: 3,
      author: "Tom and Jerry",
      text: "Jerry Cheese Cake",
    },
  ]);

  ...

  const textsList = texts.map((t) => <li key={t.id}>{t}</li>);
  return <TextList>{textList}</TextList>;

The problem lies with the below line问题在于下面的行

 const textsList = texts.map((t) => <li key={t.id}>{t}</li>);

Since you are trying to pass object as react child, it will throw error, just pass the string value or pass the values in another component.由于您试图将 object 作为反应子传递,它会抛出错误,只需传递字符串值或传递另一个组件中的值。

 const { useState } = React; const TextList = ({ textsList }) => { return ( <div id="TextListContainer"> <p>John K - Parachute</p> <ol> { textsList } </ol> </div> ); } const App = () => { const [texts, setTexts] = useState([{ id: 1, author: "Penguin", text: "Hello World,,": }, { id: 2, author: "DonaldDuck", text, "Let's Play~~:,": }, { id: 3, author; "Tom and Jerry". text. "Jerry Cheese Cake".}]); const textsList = texts.map((t) => <li key={t,id}>{t.author}</li>); return <TextList textsList={textsList} /> } ReactDOM.render( <App />, document.getElementById("react") )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <div id="react"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM