I am very new to React and Redux and I am currently trying to wrap my head around how states are shared through the UI.
I have a pretty simple use case.
Picture a container with three columns:
A left Side Menu with four buttons
A central content section
A right container with some other content
When I click one of the four buttons I want three things to happen:
The class on the button is updated so it has borders
The class on the other buttons is updated so if they had been clicked before their border class is removed
The content in one part of the middle section is updated. This content requires data from the backend (Django Rest) to be fetched
Here are my questions:
Should I separate this interaction into a UI only concern and a Data driven one or handle both with a single state?
Because the buttons share a parent component but they do not with the central component, would I be better off using Redux if I do not want to define the state at the very top level of the Container?
Because I am new and Hooks seem to be the way to go I am trying to use hooks rather than classes, does it still make sense to separate components and containers?
You can use react-router-dom library to achieve want you want.
for your left side menu you can create active class in css and then check for current route with location.pathname property of react-router-dom and if it is for that button, active class set for it.
and for your main section you can fetch data in useEffect hook.
I think it is good to separate data and UI but for buttons what I say is good.
and you can store data in redux for your main section.
Related
So I'm currently doing Brad Traversys 50 projects in 50 days, but I wanted to do it built in Next.js with React for practice, as that's what I use at my job.
As you can see from me posting here, it's not going so well! I'm already running into road blocks.
I'm trying to create a set of cards, that when one is clicked, it expands out. Outside this, if there is a card already expanded, and another card is clicked, I need the previously expanded card to collapse, whilst the card currently clicked expands.
I'm currently trying to work off an active state, and passing that down to the Panel props, but obviously that just sets all the flex's to be the same and nothing happens. But when I try to define the active useState inside the Panel component, it works, but I can't then target the other active states in the other Panels, to set them to false.
You can see my code on this CodeSandbox https://codesandbox.io/s/nifty-goldberg-5noi4?file=/pages/expanding-cards.jsx
You can see the correct functionality here https://50projects50days.com/projects/expanding-cards/
What's the best way to go about this?
You need a logic like Accordion control in Material UI. As in my comment, here is the example.
https://material-ui.com/components/accordion/#controlled-accordion
I'm trying to build a dynamic form where the data sent from the server will be rendered by the client. The form will have X amount of steps, decided by the data.
I have my component Form rendering X amount of components Steps.
My problem now is that since it's all the same component, Form, there's not possible for the user to click on the back button to go to the previous step in the Form. I somehow need to keep my URL in sync with my Form/Steps.
What would be the best solution to this problem? Using HashRouter and using Route with "/:id/:step"(how would this work)? Pushing the routes in automatically using useHistory-hook?
The most simplest case as I think is to create parent component with state step and change it when a user go or return to step. Based on step you should render the appropriate step.
A css solution would be to break the form into sections and show 1 section at a time, when some one clicks on the navigation buttons of the form a state update triggers another section to be shown while hidihng the current one. This can be done using CSS transitions, you can look into react-transition-groups to make css transitions with react easier
I went through laracasts vuejs tutorials few times, thenetninja on youtube and few others..
So I decided to change my website front into vue because of reactivity.
Like for toggles and other stuff. But there are also components which are very useful and cool but in my case it seems like they are mostly unneeded.
Every tutorial is about basic things but in my case where I have:
<component-a>
<component-b></component-b>
</component-a>
<component-c></component-c>
And inside <component-b> I click on button and with that I need to change something in <component-c>.
I don't know how to do this besides adding everything inside one component which is not the case because some things are not supposed to be rendered if user is not logged in:
<component-c>
#if(auth()->user())
<component-d></component-d>
#endif
<component-e></component-e>
</component-c>
And I can't use php inside .vue :D
So I leave everything as it is and make reactivity based on that without using components.
Is that bad?
If it is, how to alter component-d on some click inside component-b with using components?
(In my case, B is dropdown menu from topbar and D is tab section that changes on that dropdown menu item clicks)
If certain content must be hidden from the client you can't use vue and just keep using php and laravel blade's #if, but then "reactivity" can only take place on a full page load.
If the content must only be hidden visually, of can be retrieved via an API then you can use Vue directives like v-if, v-show and use javascript to show and hide components.
I was wondering about what a better practice to add the nav side bar to my project, since its an angular2 im using components, so my dashboard has a main wrapper thats splits the content and and navbar itself.
both should be components.
now, when i want to navigate to other component, I can:
1. use a link to other component that holds the same navbar (duplicate code)
OR
2. change the html page to show another component (dynamic switching when click only for the content)
what do you think ? thanks!
I'm building a single page application with React and Backbone and am trying to figure out best practices for handling navigation between content in the app. My app will have a sidebar with links, for example, to "photos" and "settings". The sidebar is always present, so upon clicking "settings" I want the settings component to be rendered without the entire page reloading. Slack is a great example of what I'm looking for, where clicking a different channel switches the conversation content, but does not reload the entire page.
I had a few ideas on how to implement this, but i'm not sure what's best:
Have a general react component (console.jsx) that accepts a urlparameter as a prop to determine which content (photos or settings) to render.
or 2. have a general react component and define a state variable that represents which content to render.
Any help would be greatly appreciated!
Have you heard about react router? Seems to be what you want.
react router