简体   繁体   English

如何使用 React 渲染 html 按钮的新组件 onClick?

[英]How can I render a new component onClick of a html button using react?

I have a component that contains a html button that when clicked on needs to call a function that renders another component and sets the state of the new component to the arguement passed in to the function that returns the new component. I have a component that contains a html button that when clicked on needs to call a function that renders another component and sets the state of the new component to the arguement passed in to the function that returns the new component. I set a state variable that is initialized with the string 'start'.我设置了一个用字符串“start”初始化的 state 变量。 When the html button is clicked the state var should change and the new component needs to be rendered.当单击 html 按钮时,state var 应该更改并且需要渲染新组件。 In my app the page doesn't load and in the minimal reproducible example I tried to make in codesandbox the state variable won't even initialize with the 'start' string在我的应用程序中,页面未加载,并且在我尝试在代码和框中制作的最小可重复示例中,state 变量甚至不会使用“开始”字符串进行初始化

import Member from "./member.js";
import { React, useState } from "react";

const Post = () => {
  const [start, setStart] = useState("start");

  const getProfile = (member) => {
    //    const addr = dispatch({ type: 'ADD_MEMBER', response: member })
    console.log(member);
    setStart(member)
    return <Member user={member}></Member>;
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button onClick={() => getProfile('ssssss')}>Profile</button>
    </div>
  );
};

export default Post;

this is the component I need to render on click这是我需要在点击时呈现的组件

import { React, useState } from "react";
const Member = (props) => {
    const [user, setUser] = useState({});

    return (
      <div class="container">
lolololololololololololololo
      {this.user}
      </div>
    )
  }
  
  export default Member;

sandbox沙盒

There are many problems with your code.你的代码有很多问题。

  1. As stated in the comments you can't use hooks outside of a functional component.如评论中所述,您不能在功能组件之外使用钩子
  2. By returning a Component from the getProfile function you are not doing anything because this component has no designated place to be rendered in.通过从getProfile function 返回一个组件,您没有做任何事情,因为该组件没有指定的渲染位置。
  3. The start variable is useless in your case because you don't change it anywhere. start变量在您的情况下是无用的,因为您不会在任何地方更改它。
  4. In your Member component you are using this.user which is wrong in a functional component.在您的Member 组件中,您使用的this.user在功能组件中是错误的。 This is only used in class components这仅用于class 组件

A somehow working version of your code may be this:您的代码的某种工作版本可能是这样的:

import Member from "./member.js";
import { React, useState } from "react";

const Post = () => {
  const [data, setData] = useState('');

  const getProfile = (member) => {
    //    const addr = dispatch({ type: 'ADD_MEMBER', response: member })
    setData(member);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button onClick={() => getProfile('Johny')}>Profile</button>
      {data && <Member user={data}></Member>}
    </div>
  );
};

export default Post;

import { React, useState } from "react";

const Member = (props) => {
  const [user, setUser] = useState(props.user);

  return (
    <div class="container">
      lolololololololololololololo
      {user}
    </div>
  );
};

export default Member;

Essentially when the button is clicked the value changes and is no longer falsy so the Member Component is rendered and gets the values as props from the data variable.本质上,当单击按钮时,值会发生变化并且不再是虚假的,因此会渲染成员组件并将值作为道具从数据变量中获取。

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

相关问题 React - 如何使用 OnClick 在 class 中渲染组件? - React - How can I render a component within a class by using OnClick? 如何在 React 中使用 onClick() 函数渲染不同的组件? - How can I render different component using onClick() function in React? 如何从 function 返回 React 组件并渲染它 onClick? - How can I return a React Component from a function and render it onClick? 如何从 React 组件中的另一个文件呈现 HTML? - How can I render HTML from another file in a React component? 如何在我的 React 组件中使用脚本标记呈现 Html? - How can I render Html with script tag in my React Component? 如何让我的 React 组件在退出 HTML 时呈现 - How can I get my React component to render on exiting HTML 我是新来的反应。 我想在反应 JS 中使用 onClick 事件使用单个 state 呈现子组件 - I am new to react. I want to render a child component using a single state using an onClick event in react JS 在 react js 中渲染新组件 onClick 的方法 - Way to render a new component onClick in react js 如何使用Jest测试React组件的onClick行? - How can I test an onClick line of a React component using Jest? 如何使用另一个组件中的按钮渲染组件 onClick? - How to render a component onClick with a button in another component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM