简体   繁体   English

React JSX Onclick组件

[英]React JSX Onclick Component

I'm new to coding I want to display my Form.jsx to my AddWord.jsx Form.jsx is a form Addword.jsx is a button in Addword.js onclick i have to display Form.jsx help me 我是编码新手,我想向我的AddWord.jsx显示Form.jsx。Form.jsx是窗体Addword.jsx是Addword.js中的按钮onclick我必须显示Form.jsx帮助我

//This is my code
import React from 'react'
import App from '../App'
import Form from './Form'

function AddWords() {
    return (
        <div>
           <button type="button" onclick="">Click Me!</button>
        </div>
    )
}

export default AddWords

Rearrange your code a little so that you can have a reusable button component, and a parent component ( App ) where both the form and the button sit. 稍微重新排列代码,以便可以有一个可重用的按钮组件,以及一个将表单和按钮都放在其中的父组件( App )。

In this example form visibility is stored in App state (using the useState React hook ) and a button handler is passed down to the button in its props to toggle that state true / false which shows/hides the form. 在此示例中,表单可见性存储在App状态中(使用useState React hook ),并且按钮处理程序在其props中向下传递给按钮以切换显示/隐藏表单的状态true / false

 // This example uses the the `useState` hook for // storing state const { useState, Fragment } = React; // A simple form component function Form() { return <div>Form</div>; } // AddWords is now a reusable button component // that accepts a button handler function, and some text // for the button. When `onClick` (don't forget that // it's camelCase) is called it calls the handler // that was passed down to it. function Button({ handleButton, text }) { return ( <div> <button type="button" onClick={handleButton} > {text} </button> </div> ); } function App() { // Initialise the state to false. `isFormVisible` is the // variable that holds the state, `setFormVisible` is the // function that allows us to update it. const [ isFormVisible, setFormVisible ] = useState(false); // The handler that we pass to the button. It toggles // the `isFormVisible` state from `true` to `false`, or // `false` to `true. function toggleForm() { setFormVisible((visible) => !visible); } // We return a fragment wrapping our child // Form and Button components. Note that we're // passing the toggleForm function and text down as // Button props. // The form is only visible if `isFormVisible` is true. return ( <Fragment> <Button handleButton={toggleForm} text="Click me!" /> {isFormVisible && <Form />} </Fragment> ); } // Render it ReactDOM.render( <App />, document.getElementById('root') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> <div id="root"></div> 

You may convert your functional component to class component and your code will be more precise and readable. 您可以将功能组件转换为类组件,并且代码将更加精确和易读。 Here I have used state which is bind with this react app, if any change react dom element will render based on change. 在这里,我使用了与此React应用程序绑定的state ,如果有任何更改,则React dom元素将基于更改呈现。

One more thing I want mention here is {this.state.isVisible && <Form />} , in javascript if 1st part returns truthy the next part will execute otherwise not, so here if this.state.isVisible = true only then Form will be visible 我还要在这里提到的另一件事是{this.state.isVisible && <Form />} ,在javascript中,如果第一部分返回true,则下一部分将不执行,因此如果this.state.isVisible = true,则仅Form将执行可见

import React, { Component } from "react";
import Form from "./Form";

class AddWords extends Component {
  state = {
    isVisible: false
  };

  handleClick = () => {
    this.setState({ isVisible: true });
  };

  render() {
    return (
      <div>
        <h1>AddWords</h1>
        <button onClick={this.handleClick}>Click Me!</button>
        {this.state.isVisible && <Form />}
      </div>
    );
  }
}

export default AddWords;

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

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