Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I am very new to React. I am trying to make a reusable Checkbox component. These checkboxes are to send info to an API. I have a GET command on App.JS, to set state.
GET
App.JS
Here is a simplified version of my API
{ "devices":[ { "id":1, "valveA": true, "valveB": false, "valveC": true, }, { "id":2, "valveA": false, "valveB": true, "valveC": false, } ] }
I pass the props to the children like so:
render() { const {devices} = this.state; return ( <div > {devices.map(device => ( <Device device={device} key={device.id} /> ))} div> );
I can make individual components for each checkbox and setting the checked state by setting individual state like so:
checked
state = { checked: this.props.device.valveA }
But that means I have to make a component for each 'valve' in my API. Ideally I would like to have one Checkbox component that I can reuse for all my "valves".
I've made a semi-working component by specifying the name of the valve as a prop:
<Device device={device} key={device.id} switchFor="valveA" />
And here is my component that successfully passes the change to the API, however I need to dynamically set the last part of the setState
this.props.device.{{{I WANT THIS TO BE DYNAMIC}}}
, otherwise all switches just get the state of valveA:
state = { checked: false }; componentDidMount(){ this.setState({ checked: this.props.device.valveStatus }) } handleCheckboxChange = event => { const type = this.props.switchFor; const checkedStatus = event.target.checked; const deviceID = this.props.device.id; const obj = {}; obj[type] = checkedStatus; this.setState({ checked: checkedStatus }); axios REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
Your explanation isn't really quite clear, but I think I can KIND OF understand what you're trying to do. If I'm wrong, just ignore me. So it seems like you want to be able to change which valve/switch of the device object to be displayed by your component. If that's the case, simply set that as a state. Off the top of my head, I can think of using a an array that mirrors your "devices" array:
this.state = { "devices":[ { "id":1, "valveA": true, "valveB": false, "valveC": true, }, { "id":2, "valveA": false, "valveB": true, "valveC": false, } ], "selectedValve": ["valveA", "valveB"] }
Then when rendering your Device components, just do:
render() { const {devices} = this.state; return ( <div > {devices.map((device, ind) => ( <Device device={device} key={device.id} switchFor={this.state.selectedValve[ind]} /> ))} div> ); }
To change the valve of a particular device, you can have:
handleSwitchChange(deviceInd, newValve){ let copySelectedValve = [...this.state.selectedValve]; copySelectedValve[deviceInd] = newValve; this.setState({ selectedValve: copySelectedValve; }) }
To change the first device to valveB, you'd just do handleSwitchChange(0, "valveB"); To change the true/false of a valve of a device, you can write something like:
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.