简体   繁体   English

我如何使用道具以在ReactJS中创建条件,在此条件下我可以更改应用程序的状态

[英]How do I use the props in order to create a condition in ReactJS where I am able to change the state of my app

I have two components, the parent "App" component and the Hero component which contains an image and left and right arrow. 我有两个组件,父组件“ App”和包含图像以及左右箭头的Hero组件。 I want to use the right arrow to move the imageIndex + 1 until it reaches the images.length, and I want to have the left arrow to have a condition that I can't subtract if imageIndex = 0. 我想使用向右箭头移动imageIndex +1直到到达images.length,并且我想让向左箭头具有条件,如果imageIndex = 0,则无法减去。

So something like: ( This part of the code is not added in my code yet because I keep getting undefined) 就像这样:(这部分代码尚未添加到我的代码中,因为我一直未定义)

   if (this.props.imageIndex > 0) {
     this.setState({
       // decrease the imageIndex by 1
     })
   } 

   if (this.props.imageIndex < this.props.images.length - 1){
     this.setState({
       // increase the imageIndex by 1
     })
   }

will be the condition or something like it. 将是条件或类似的东西。

App.jS (Parent Component) App.jS(父组件)

export default class App extends Component {
constructor() {
    super();

    this.state = {
        language: "english",
        render: 'overview',
        imageIndex: 0,
    }
}

render() {
    // to make sure the state changes
        console.log(this.state.language)

    const {render} = this.state
    return <Fragment>
        <Hero imageIndex = {this.state.imageIndex} />
    </Fragment>;
   }
}

How would I add that in my Hero Component which contains this code: 我如何在包含以下代码的Hero组件中添加它:

Hero.js Hero.js

class Hero extends Component {
constructor(props) {
    super(props);
    this._ToggleNext = this._ToggleNext.bind(this);

}


  _ToggleNext(props) {
    console.log(this.props.listing.images.length)
    console.log(this.props.imageIndex)
}

  _TogglePrev(props) {
    console.log(this.props.listing.images.length)
    console.log(this.props.imageIndex)
}

render() {
    const { listing: { images = [], name, location = {} } = {} } = this.props;
    return <div className="hero">
        <img src={images[0]} alt="listing" />
        <a onClick={this._TogglePrev}) className="hero__arrow hero__arrow--left">◀</a>
        <a onClick={this._ToggleNext} className="hero__arrow hero__arrow--right">▶</a>
        <div className="hero__info">
            <p>{location.city}, {location.state}, {location.country}</p>
            <h1>{name}</h1>
        </div>
    </div>;
}
}

const getHero = gql`
query getHero {
    listing {
        name
        images
        location {
            address,
            city,
            state,
            country
        }
    }
}
`;


export default function HeroHOC(props) {
return <Query
    query={getHero}
>
    {({ data }) => (
        <Hero
            {...props}
            listing={data && data.listing || {}} // eslint-disable-line no-mixed-operators
        />
    )}
</Query>;
}

One solution is to define the data and functionality in the parent component, in this case App , and pass those down as props to the child which will focus on the rendering. 一种解决方案是在父组件(在本例中为App )中定义数据和功能,并将其作为props向下传递给将专注于渲染的子组件。

(code not tested but should give you the basic idea) (代码未经测试,但应该给您基本概念)

class App extends Component {
    state = {
        imageIndex: 0,
        listing: {
            images: ['foo.jpg', 'bar.jpg'],
            name: 'foo',
            location: {...}
        }
    }
    _ToggleNext = () => {
        const { imageIndex, listing } = this.state;
        if (imageIndex === listing.images.length - 1) {
            this.setState({imageIndex: 0});
        }
    }
    _TogglePrev = () => {
        const { imageIndex, listing } = this.state;
        if (imageIndex === 0) {
            this.setState({imageIndex: listing.images.length - 1});
        }
    }
    render() {
        return (
            <Fragment>
                <Hero
                    listing={this.state.listing}
                    imageIndex={this.state.imageIndex}
                    toggleNext={this._ToggleNext}
                    togglePrev={this._TogglePrev}
                />
            </Fragment>
        );
    }
}

Hero component: 英雄部分:

const Hero = props => {
    const { listing, imageIndex, togglePrev, toggleNext } = props;
    return (
        <div className="hero">
            <img src={listing.images[imageIndex]}/>
            <a onClick={togglePrev})>◀</a>
            <a onClick={toggleNext}>▶</a>
            <div className="hero__info">
                ...
            </div>
        </div>
    );
};

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

相关问题 我无法理解道具在 reactjs 中的工作原理 - i am not able to understand how props works in reactjs 如果我的道具在 React 中发生变化,如何更新 state 以强制重新渲染? - How do I update the state to force a rerender if my props change in React? 我的 Reactjs 应用程序中是否需要全局 state? - Do I need a global state in my Reactjs app? 为什么我无法在我的基本React应用中使用Promise? - Why am I not able to use promise in my basic react app? 我无法在 React.js 中更改我的 state 对象的 state - I am not being able to change the state of my state objects in React.js 为什么我的React孙子组件在收到其父组件的道具更改后没有更新? 或者:我需要使用状态吗? - Why does my React grandchild component not update after receiving a change in props from its parent? Or: do I need to use state? reactjs、setState、Interface props 和 Interface state 之间的关系相对于 typescript,如何使用 setState? - relation between reactjs ,setState, Interface props and Interface state with respect to typescript , how can I use setState? ReactJS,Redux:如何在STATE中返回一组项目? - ReactJS, Redux : How do I return an array of items in my STATE? 为什么我在道具改变后得到旧的状态值 - Why I am getting the old state values after the props change 我如何将 Json 数据发布到我的状态 ReactJs - How do i post a Json data into my state ReactJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM