【问题标题】:Display the selected item to another component using react hooks使用反应钩子将所选项目显示到另一个组件
【发布时间】:2021-07-05 04:36:28
【问题描述】:

我有一个包含模板的部分,现在我希望用户选择其中一个模板并编辑模板中的数据,例如姓名、职位、联系方式等。

这是我在代码沙箱中的现场演示editor live demo

这里是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>
  );
}

这里是 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>
  );
}

预期结果:

问题:我不知道如何在设置组件中显示选择的模板。

如何将选定的模板复制到设置组件,以便用户可以编辑选定的模板?

【问题讨论】:

    标签: javascript html reactjs forms react-hooks


    【解决方案1】:

    我不知道为什么,但是有人删除了我的其他帖子,这就是我现在看到您的消息的原因。如果要制作不同的样式,可以将类名添加到数据中。另外,如果您愿意,可以查看这个库:https://material-ui.com/styles/basics/ 它在 react 用户中非常受欢迎。您基本上可以使用这个库在 javascript 中制作所有样式。如果你要使用这个库,你可以在你的数据中添加所有的 jss。 这是一个例子:

      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
      }
    ]

    以及如何使用这些样式的基本解释

    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);

    【讨论】:

    • 没有任何理由我应该将数据保存在父组件中,而我将拥有数百个模板,将所有内容都保存在父组件中是一个坏习惯,这里的问题不是应用程序结构,问题是如何将所选元素显示到设置组件,
    • 我认为这是您想要的:codesandbox.io/s/templates-forked-hlhnd?file=/src/Components/…(我更改了设置、应用程序和索引。现在您可以在表单下看到您选择的项目)我刚刚从模板中移动了 'selected' 和 'setSelected' .js 到 index.js 所以现在所有组件都可以使用 'selected' 和 props 。我将在几个小时内给出基于数据的表格模板的示例。
    • 这里:codesandbox.io/s/templates-forked-y3e7m 它有效。现在您可以使用 changeOnData 函数更改数据。我认为你的问题是使用 js-dom。您应该使用反应状态而不是 js-dom 来更改和获取值。
    • 没问题很高兴我能帮上忙 :)
    • 有人删除了我的另一篇文章,所以我用你最后一个问题的答案编辑了这篇文章。
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2020-10-13
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多