1

I'm a beginner of react please help to resolve this issues

Gate.js


    function sendData(send) {
        props.setGet(send);
    }
...
                <SwiperSlide id='slide01'>
                    {({ isActive }) => (
                    <div className='nav_title'>
                        { isActive === true ? sendData("1") : null}
                        ...
                    </div>
                    )}
                </SwiperSlide>
}

Apps.js

const [get, setGet] = useState('');
...   
      <Gate setGet={setGet}/>
      <Contents value={get}/>
...

Contents.js

const value = props.value;

...

<div className='contents'>
            {value === "1" ? <div className='show_animation'><Offers/></div> : null}
            {value === "2" ? <div className='show_animation'><Booking/></div> : null}
            {value === "3" ? <div className='show_animation'><About/></div> : null}
            {value === "4" ? <div className='show_animation'><Guide/></div> : null}
        </div>
...

I wanna make like this
enter image description here

2 pages connecting automatically

2
  • Can you give more context on what you're trying to achieve?. You should not call setGet directly in the component markup, that causes a state update every time the component renders. You should enclose that setGet call in an event handler, and attach it to something like an onClick. If you can provide more context on what is that you're intention is with that setGet call I could elaborate an answer Commented Aug 1, 2022 at 3:04
  • 1
    Welcome to StackOverflow, please copy/paste your code (the part of the problem) into the question, and format it using the code formatted button at the top of the edit page of the question. and please do not paste images of code Commented Aug 1, 2022 at 3:33

1 Answer 1

0

This is a simplified version of what I think the component structure of your code looks like based on the photos.

and I'm also not sure if this is what you are trying to achieve. If you intend on sharing state between components and updating that value in children the components, This is how you could do it.

You should always be conscious of which components will need state and try to put your stateful logic in the parent of all the children which need it.

and once you have that then you need to decide which child components will update this state, If it happens to be in the Contents component as it is in this example, then you need pass a function prop to that child. Which is onSomethingChanged={handleChange} in this example. and then to update the handleChange function, you need to use a useCallback in the child component that calls the onSomethingChanged function.

const App = () => {

    const [statefulLogic, setStatefulLogic] = useState('');
    //because state is updateed in this parent component, any children receiveing props will update with this;

    const handleChange = (props) => {
        setStatefulLogic(props)
    }

    return (
        <>
            <Gate yourprops={statefulLogic} />
            <Contents onSomethingChanged={handleChange} yourprops={statefulLogic}/>
            {/** when useCallback is called this function /handleChange/ will be called with the arguments given in the useCallback  */}
        </>
    )
}
const Gate = (props) => {

    useEffect(() => {
        console.log(props.yourprops)
    }, [props]) // when props is updated this useEffect will run
    
    return (
        <>
            <children />
            <children />
        </>
    )
}
const Contents = (props) => {

    const newValue = ''; 

    const handleCLick = useCallback(() => {
        props.onSomethingChanged(newValue)
    }, [props]) // this dependency may be different depending on your use case
    // this useCallback will pass the newValue back to the parent component


    return (
        <>
            <children />
            <children />
            <button onClick={handleClick}>Click me</button>
           // when this is clicked, the useCallback updates state in the parent component
           //  and that updates the state in all children which are using that state 
        </>
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for reply :) could you please check my code again? I upload my new codes and what I want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.