简体   繁体   English

使用反应钩子将所选项目显示到另一个组件

[英]Display the selected item to another component using react hooks

I have a section in which contains templates, now I want a user to select one of the templates and edit the data in templates eg name, job title, contact etc.我有一个包含模板的部分,现在我希望用户使用其中一个模板 select 并编辑模板中的数据,例如姓名、职位、联系方式等。

Here is my live demo in code sandbox editor live demo这是我在代码沙箱编辑器现场演示中的现场演示

Here is templates.js这是templates.js

export default function Templates() {
  const [selected, setSelected] = useState("");
  let history = useHistory();

  const handleSelected = (e) => {
    const value = e.target.innerHTML;
    setSelected(value);
   history.push('/Settings') 
  };


  return (
    <div className="App">
      <h2>Select one of the Template below by clicking title</h2>

    <div className={`template_one ${selected === "Template_one" ? "selected" : "unselected"}`} onClick={(e) => {setSelected(selected !== "Template_one" ? "Template_one" : ""); handleSelected(e);}}>
        <h1>Template_one</h1>
        <table striped bordered hover size="sm">
          <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Job</th>
              <th>Facebook</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Mark</td>
              <th>Developer</th>
              <td>facebook/Mark</td>
            </tr>
          </tbody>
        </table>
   
      </div>

      <div className={`template_two  ${selected === "Template_two" ? "selected" : "unselected"}`} onClick={(e) => {setSelected(selected !== "Template_two" ? "Template_two" : "");handleSelected(e);}}>
        <h1>Template_two</h1>

        <table striped bordered hover size="sm">
          <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Job</th>
              <th>Company</th>
              <th>Contact </th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Trump</td>
              <th>President</th>
              <td>Trump Organization</td>
              <td>+1 728 256 345</td>
            </tr>
          </tbody>
        </table>
       
      </div>
    </div>
  );
}

And here is settings.js这是settings.js

import React from "react";

export default function Settings() {
  return (
    <div>
      <h1>Settings</h1>
      <div className="Settings">
        <form>
          Name: <input type="text" name="firstname" />
          Job: <input type="text" name="job" /> <br /> 
          Facebook: <input type="text" name="facebook" /> 
          Company: <input type="text" name="company" />  
          Contact: <input type="text" name="contact" /> 
        </form>
        <div className="selected-template">
          Here should be the selected template
        </div>
      </div>
    </div>
  );
}

Expected result:预期结果:

在此处输入图像描述

Problem: I don't know how to display the selected templates in the settings component.问题:我不知道如何在设置组件中显示选定的模板。

How do I copy the selected template to the settings component so that the user can edit the selected template.?如何将选定的模板复制到设置组件,以便用户可以编辑选定的模板。?

I don't know why but someone deleted my other post that's why I'm seeing your message now.我不知道为什么,但是有人删除了我的其他帖子,这就是我现在看到您的消息的原因。 If you want to make different styles you can add classnames to your data.如果您想制作不同的 styles 您可以将类名添加到您的数据中。 Also if you want, you can look at this libary: https://material-ui.com/styles/basics/ it is so populer around react users.另外,如果您愿意,可以查看以下库: https://material-ui.com/styles/basics/它在反应用户中非常受欢迎。 You can basicly make all styles in javascript with this libary.您基本上可以使用此库在 javascript 中制作所有 styles。 If you gonna use this libary you can add all jss in your data.如果你要使用这个库,你可以在你的数据中添加所有的 jss。 here is example:这是示例:

 const table1= { root: { background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', border: 0, borderRadius: 3, boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)', color: 'white', height: 48, padding: '0 30px', }, }; Data=[ { name:'something', style:table1 }, { age:22, style:table2 } ]

and basic explanation about how to use this styles以及有关如何使用此 styles 的基本说明

 import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; function Table() { const classes = props.data.style(); return <Table className={classes.root}>Your table here</Table>; } export default withStyles(props.data.style)(Table);

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

相关问题 如果选中/未选中复选框,则将项目添加/删除到数组中? (挂钩,不是基于 class 的组件) - React add/remove item to array if checkbox is selected/not selected? (Hooks, not class based component) 在将新项目添加到数据库后,如何使用 React 钩子更新 React 中的组件? - How can I update component in React after adding a new item into the database, using react hooks? 从另一个组件反应钩子设置状态 - Setting state from another component react hooks 输入值未呈现到 React Hooks 中的另一个组件 - Input values not rendering to another component in React Hooks 使用react-router选择新项目时刷新组件 - Refresh component when new item selected using react-router 我需要一个组件来检查状态并在另一个组件更改后重新渲染(使用 React Hooks) - I need a component to check state and rerender after another component changes (using React Hooks) 在按钮单击中显示组件(使用挂钩) - display a component in button click (using hooks) 每次单击时显示不同的组件(使用挂钩) - to display a different component with each click (using hooks) 使用 React-Hooks 单击按钮后如何显示不同的组件? - How to display different component after button click using React-Hooks? 从 React 组件转换为使用一个钩子 - convert from React component to using one hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM