Html ReactJS:如何将道具传递给造型?
我有以下部门:Html ReactJS:如何将道具传递给造型?,html,reactjs,z-index,Html,Reactjs,Z Index,我有以下部门: <div className='box-default expand' style={{zIndex: props.zindex}}> 当我检查元素时,结果是: <div zindex="0">...</div> 。。。 但它不起作用。难道不是: <div style="z-index: 0">...</div> 。。。 ? 这是父容器: const [zIndex, setZIndexes] = Reac
<div className='box-default expand' style={{zIndex: props.zindex}}>
当我检查元素时,结果是:
<div zindex="0">...</div>
。。。
但它不起作用。难道不是:
<div style="z-index: 0">...</div>
。。。
?
这是父容器:
const [zIndex, setZIndexes] = React.useState([0, 1, 2]);
function setZIndex(id) {
setZIndexes([0, 0, 0]);
zIndex[id] = 3;
console.log(zIndex);
}
<div
zindex={zIndex[0]}
onClick={()=>setZIndex(0)}
>
const[zIndex,setzindex]=React.useState([0,1,2]);
函数setZIndex(id){
setzindex([0,0,0]);
zIndex[id]=3;
控制台日志(zIndex);
}
setZIndex(0)}
>
将道具沿组件链传递:
但是父组件应该返回like,而不是div
const ParentComponent = () => {
...more code
return (
<ChildComponent zindex={zIndex[0]} />
)
}
constparentcomponent=()=>{
…更多代码
返回(
)
}
和ChildComponent函数应包含:
const ChildComponent = (props) => (
<div className='box-default expand' style={{zIndex: props.zindex}} >...</div>
)
constchildcomponent=(道具)=>(
...
)
您可以试试这个
const ParentComponent = ({ message }) => {
const ChildComponent = zindex={zIndex[0]}
const ChildComponent = (props) => (
<div className='box-default expand' style={{zIndex: props.zindex}} >
{message}</div>
)
}
constparentcomponent=({message})=>{
常量ChildComponent=zindex={zindex[0]}
常量ChildComponent=(道具)=>(
{message}
)
}
style={{zIndex:props.zIndex}}
应该可以工作zindex={zindex[0]}
不会