繁体   English   中英

如何将值从状态传递到另一个组件

[英]How to pass values from a state to another component

我的 React 应用程序上有两页。 一页允许您提交帖子,第二页显示所有帖子。 我需要能够从一页上的状态中检索数据,但我收到一个错误。 显示这个我做错了什么,因为我认为我可以使用道具从我的帖子页面收集状态。

我的显示帖子页面:

import React from 'react';
import './App.css';


export default class Scroll extends React.Component {


    render() {
        return (

            <div className="flex-container">
                <div className="post">
                    {this.props.displayPost(this.props.state.posts)}
                </div>
            </div>
        );
    }
}

我的帖子页面:

import React from 'react';
import axios from 'axios';
import './App.css';
import { post } from '../../routes/routes';

export default class PersonList extends React.Component {

    state = {
        title: "",
        body: "",
        posts: []
    };

    componentDidMount = () => {
        this.getPost();
    }

    getPost = () => {
        axios.get("http://localhost:5000/posts/save")
            .then((response) => {
                const data = response.data;
                this.setState({ posts: data });
                console.log("Data has been recieved")
            })
            .catch(() => {
                alert("Error recieving data")
            })
    }

    handleChange = (event) => {
        const target = event.target;
        const name = target.name;
        const value = target.value;

        this.setState({
            [name]: value
        })
    };

    submit = (event) => {
        event.preventDefault();

        const payload = {
            title: this.state.title,
            body: this.state.body,
        }

        axios({
            url: 'http://localhost:5000/posts/save',
            method: 'POST',
            data: payload,
        })
            .then(() => {
                console.log('Data sent to the server');
            })
            .catch(() => {
                console.log('Internal server error');
            });
    };

    displayPost = (posts) => {
        if (!post.length) return null;

        return posts.map((post, index) => {
            <div key={index}>
                <h3 id="post-text">{post.title}</h3>
                <p id="post-text">{post.body}</p>
            </div>
        });
    }

    render() {
        console.log("State ", this.state)
        return (
            <div className="flex-container-home">
                <div className="app">
                    <form onSubmit={this.submit}>
                        <input
                            placeholder="title"
                            type="text"
                            name="title"
                            value={this.state.title}
                            onChange={this.handleChange}
                        />
                        <textarea placeholder="description"
                            name="body"
                            cols="30" rows="10"
                            value={this.state.body}
                            onChange={this.handleChange}
                        >
                        </textarea>
                        <button>Submit</button>
                    </form>
                </div>
            </div>
        )
    }
}

这是工作示例:

import React from "react";

export default class PersonList extends React.Component {
  state = {
    title: "",
    body: "",
    posts: [],
  };

  componentDidMount = () => {
    this.getPost();
  };

  getPost = () => {
    this.setState({ posts: ["post1", "post2", "post3"] });
  };      

  displayPost = (posts) => {
    if (!posts || !posts.length) return null;

    return posts.map((post, index) => (
      <div key={index}>
        <p>{post}</p>
      </div>
    ));
  };

  render() {
    return (
      <div className="App">
        <Scroll displayPost={this.displayPost} posts={this.state.posts} />
      </div>
    );
  }
}

class Scroll extends React.Component {
  render() {
    return (
      <div className="post">
        Posts: {this.props.displayPost(this.props.posts)}
      </div>
    );
  }
}

暂无
暂无

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

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