Reactjs 如何将数据从此处传输到此处
我想把我在一个组件中得到的道具转移到另一个组件中Reactjs 如何将数据从此处传输到此处,reactjs,Reactjs,我想把我在一个组件中得到的道具转移到另一个组件中 function StarWar(props) { function handleClick(e) { e.preventDefault(); console.log(props.other) return <div><Battle value={props.other}/></div> } if (props.value == 0)
function StarWar(props) {
function handleClick(e) {
e.preventDefault();
console.log(props.other)
return <div><Battle value={props.other}/></div>
}
if (props.value == 0) {
return <div>
<button className="btn btn-warning starwar"
onClick={handleClick}>
Start war
</button>
</div>
}
return <h1>Choose {props.value} more avangers</h1>
}
功能星战(道具){
函数handleClick(e){
e、 预防默认值();
console.log(props.other)
返回
}
如果(props.value==0){
返回
开战
}
返回选择{props.value}更多Avanger
}
从这里我想把这些值传递给Battle,我正在handleClick(e)中尝试它
这也是我的战斗课
export class Battle extends React.Component {
constructor(props) {
super(props);
console.log(props.value);
}
render() {
return (
<div>
<h1>Battle begins here</h1>
</div>
)
}
}
export class.Component{
建造师(道具){
超级(道具);
console.log(props.value);
}
render(){
返回(
战斗从这里开始
)
}
}
它正确地看到my,但值不绑定
使用JSX和三元运算符概念来显示作战组件。
,这是您问题的有效解决方案
功能星战(道具){
函数handleClick(e){
e、 预防默认值();
console.log(props.other)
返回
}
如果(props.other==0){
返回
开战
}
返回(
{props.other!==null(
):null}
选择{props.value}更多avangers
)
}
类。组件{
状态={
值:this.props.value
};
组件将接收道具(下一步){
if(this.props.value!==nextrops.value){
this.setState({value:nextrops.value});
}
}
render(){
返回(
{this.state.value}
战斗从这里开始
);
}
}
ReactDOM.render(,document.getElementById('root'))代码>
使用JSX和三元运算符概念来显示作战组件。
,这是您问题的有效解决方案
功能星战(道具){
函数handleClick(e){
e、 预防默认值();
console.log(props.other)
返回
}
如果(props.other==0){
返回
开战
}
返回(
{props.other!==null(
):null}
选择{props.value}更多avangers
)
}
类。组件{
状态={
值:this.props.value
};
组件将接收道具(下一步){
if(this.props.value!==nextrops.value){
this.setState({value:nextrops.value});
}
}
render(){
返回(
{this.state.value}
战斗从这里开始
);
}
}
ReactDOM.render(,document.getElementById('root'))代码>
您正在从handleClick返回html元素。为什么???您正在从handleClick返回html元素。为什么???它工作不好它没有把价值观传递给战斗again@boris我已经用Battle component中的componentWillReceiveProps()更新了代码,现在它将更新为新的道具。它工作不好,它没有将值传递给Battleagain@boris我已经用Battle component中的componentWillReceiveProps()更新了代码,现在它将更新为新道具。