[英]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.