Disclaimer: sorry this is going to be a bit long
So I'm working on an intermediately complex app using Vue JS and Vuex.
This is my first Vue SPA project so I'm facing some difficulties with architectural choices, particularity with the question "who should know about the store?"
I'll describe my problem with a dummy example:
Say we have a Post
component that has 3 states to get switched with a button:
title
and text
.title
input and text
inputNow in order to show and update a post, the component gets an id
prop and selects the post object from a list of posts within the store, and dispatches actions when necessary. It has an update
internal attribute to switch between show and update states.
As for the create state, a null id
is passed to the component, so it won't fetch anything from the store and shows inputs, it dispatches insert actions.
const KnowledgebalePost = {
name: 'Post',
props: ['id'],
data() {
return {
post: {
title: '',
text: '',
},
state: 'show'
}
},
methods: {
updateClicked() {
this.state = 'update';
},
saveClicked() {
this.state = 'show';
const postObject = { id: this.id, ...this.post };
const action = this.id ? 'updatePost' : 'addPost';
this.$store.dispatch(action, postObject);
},
},
created() {
if(this.id) {
// just to simplify
this.post = this.$store.state.posts[this.id];
}
}
};
The benefits I see in this is mainly the encapsulation of everything related to the component. It knows how to get its data and it is stand alone, all I need is to pass it an id.
On the other hand, knowing too much is problematic, a lot of things outside of the scope of the component could break it.
In this approach the component would get everything as a property: id
, title
, text
, and state
to tell it if it should render inputs or just text fields.
And instead of dispatching actions it would maybe emit events.
const IgnorantPost = {
name: 'Post',
props: ['id', 'title', 'text', 'state'],
data() {
return {
post: {
title: '',
text: '',
},
internalState: 'show'
}
},
methods: {
updateClicked() {
this.internalState = 'update';
},
saveClicked() {
this.internalState = 'show';
this.$emit('saving', { id: this.id, ...this.post });
},
},
created() {
this.post.title = this.title;
this.post.text = this.text;
this.internalState = this.state;
}
};
While this solves the dependencies problem, it just pushes some of the logic to the parent component like handling if states of the Post
component.
Also if this parent has many children other than Post
, it'd become a very fat logic container.
Note that my component is a lot more complex, any ideas on how to approach this particular problem?
Thanks in advance, and I appreciate you reading so far.
In React we can access the child's state using Refs. we will assign a Refs for the child component in the parent component. then using Refs we can access the child's state. Creating Refs Refs are created using React.
You may access the child state by passing a callback to the child component. Now if you click the button in the child component, you will execute the function passed from the parent and have access to the child component's state variables.
Parent Component. While we nest the child component inside our parent component, we are binding the data we want to send by using the directive v-bind and sending the data using props. Any time the parent changes the prop, the new value is sent to the child and rerendered.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
You're pretty much in the area of "primarily opinion-based," but I'll put in my two cents: The store is a global; lean toward having things manipulate it directly, rather than putting layers between it and components.
However, if the components you're using to implement your program have much promise of reusability, you might want to implement them in the usual encapsulated way, pushing the store interaction up.
But mostly I think manipulate the store directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With