score:1
Accepted answer
I'll rewrite it a bit:
import React, { useEffect, useState } from "react";
import axios from "axios";
import { Link } from "react-router-dom";
import "../App.css";
const SingleShopOrder = () => {
const [shopOrder, setshopOrder] = useState([]);
const [message, setMessage] = useState(0); // if message is not an array
// then do not initialize the state as []
// by the way the name of the var is confusing me
// if it is supposed to store the selected order then 'message' is
// inaccurate
const fetchNormacs = () => {
axios
.get("http://localhost:8090/api/ctms/normacs/machine/NOR1")
.then((res) => {
// console.log(res);
setshopOrder(res.data);
})
.catch((err) => {
console.log(err);
});
};
// here we are expecting order's id
const handleChange = ({target:{value}}) => {
// setMessage(value); // use this line if ID is a string
setMessage(Number(value)) // use this line if ID is a number
};
const begin = () => {
console.log("Clicked");
};
useEffect(() => {
fetchNormacs();
}, []);
if (shopOrder) {
return (
<>
<div className="mainCard">
<div className="container column-left">
<div className="item">
<Link className="navStyle" to="/Overview" onClick={() => begin()}>
Overview
</Link>
</div>
<div className="item">
<Link
className="navStyle"
to="/ShopOrder"
onClick={() => begin()}
>
Shop Order
</Link>
</div>
<div className="item">
<Link className="navStyle" to="/Employee" onClick={() => begin()}>
Employee
</Link>
</div>
<div className="item">
<Link
className="navStyle"
to="/Maintenance"
onClick={() => begin()}
>
Maintenance
</Link>
</div>
</div>
<div className="column-right">
<h2 className="so_header">Select shop order for Normac 1</h2>
<div className="wc">
{shopOrder.map((so) => (
<div key={so.ID}>
<h3>S/O No :{so.ord_no}</h3>
<p>Item No :{so.item_no}</p>
<p>Item Desc :{so.item_desc_1}</p>
<button
id="message"
name="message"
value={so.ID} // se the button value to current order's id
onClick={handleChange}
>
SELECT
</button>
</div>
))}
</div>
</div>
</div>
</>
);
}
// check if message is different from the initial state
if (message !== 0) return <VerifySingleShopOrder
order={shopOrder.find(item => item.id === message)} // pass your props here
/>
return <h1>Check the KepServer tags!!!</h1>;
};
export default SingleShopOrder;
Then in your VerifySingleShopOrder component:
import React, { useEffect, useState } from 'react'
import { Link } from "react-router-dom";
import '../App.css';
// component now is expecting a prop called 'order' which have all the
// properties you need to display detailed message
const VerifySingleShopOrder = ({order:{ID,ord_no,item_no,item_desc_1}}) => {
const [message, setMessage] = useState([]);
const handleChange = event => {
setMessage(event.target.value);
}
const begin = ()=> {
console.log("Clicked")
}
return(
<>
<div className='mainCard'>
<div className="container column-left">
<div className="item">
<Link className="navStyle" to="/Overview" onClick={() => begin()}>
Overview
</Link>
</div>
<div className="item">
<Link className="navStyle" to="/ShopOrder" onClick={() => begin()}>
Shop Order
</Link>
</div>
<div className="item">
<Link className="navStyle" to="/Employee" onClick={() => begin()}>
Employee
</Link>
</div>
<div className="item">
<Link className="navStyle" to="/Maintenance" onClick={() => begin()}>
Maintenance
</Link>
</div>
</div>
<div className='column-right'>
<h2 className='so_header'>Verify shop order for Normac 1</h2>
<div className='wc'>
<h3>S/O No :{ord_no}</h3>
<p>Item No :{item_no}</p>
<p>Item Desc :{item_desc_1}</p>
<button id="message" name="message" value={"1"} onClick={handleChange}>LOGIN & VERIFY</button>
</div>
</div>
</div>
</>
)
}
export default VerifySingleShopOrder;
Of course, this code looks ugly a lot... but for a starter it's ok, you still have a lot to learn about react
I hope it's of some help
Source: stackoverflow.com
Related Query
- How to open a new page with specific dataset with a button click in react?
- How to open a page in new tab on click of a button in react? I want to send some data to that page also
- How to open an <input type="file"/> with a button click in React
- Open new page on click of button in react
- How to open React component as new page from a button in different component
- How to click a button and navigate to a new route with URL params using React Router 4
- Navigate to a new page with React when I click on a button
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- React router - pass api data to the linked component to open with a new page
- How can we implement an Open with Postman button to open a postman collection link sent from a third party react application?
- Is there any method to open page using a React Router v6 on button click
- How to change image on button click with React Hooks?
- How to fetch data on page load and then on button click with useEffect
- How to delete objects from react state hook array with a button click
- how to open datepicker on button click or icon click in react js
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- How to open a File on a specific route with React Router
- How can click on button to get one of components with react
- How to redirect another page after button on click method react js
- How to fire a setState function in react from child componenet to parent componenet with data from a button click
- How do i redirect to a page from button click in react from a component?
- React Show data with page refresh on Button click
- React router - click on card and redirect to a new page with content of that card (card details) using useParams and reacthooks
- React router - click on card and redirect to a new page with content of that card (card details)
- How to get a login button to navigate to a new page once clicked. I have downloaded and used the React router dom
- How to Open React Native Expo Camera On Button Click
- My React 18 app does not reload automatically I try to click on the create new project button does not work unless I refresh the page manually
- How to create a new page and route programatically in React.js with React Router
- Route to new component with props on button click React
- How to open up a new page when clicked on a button in React.js without using Routers?
More Query from same tag
- How do I compare my input to my state array, to prevent duplicate INPUT
- Filter nested data with array vales using lodash react js
- How can I write php scripts in react.js file?
- How to find for the particular value in Object response type
- Showing decimal point in gauge meter in d3
- Skip first useEffect render custom hook
- Make div's below each other
- Keep getter function in sync with an object's current state in React/TypeScript
- Error using axios interceptors and responses
- create five bullet points programmatically in react? Users can add tasks, but I want to start with initial variable number of bullets
- How can I use Mocha to unit-test a React component that paints on a <canvas>?
- HTTP ERROR 405 when deploying React frontend with Spring boot REST API backend
- React notification system
- How to display time with momentjs in react
- Bootstrap's toggle button group not calling onChange event in react
- Framer Motion animation repeats on state change
- OnClick event on Link component
- key parameter issue in ReactJS Hooks, updating all Products items when using (add/subtract) indivisually
- Specify time of events sent with AppInsights?
- How to set parent callback to child state
- Set Debug or Release | React Native - iOS
- Pass values from child component to parent component - pick quantity values in child component rendering them on the parent component as a sum
- PrevState does not update state value immediately in React JS
- How should I correctly use setState for the following object
- Add class to Accordion Toggle when collapsed
- React-router match params and forward button
- How to make loading animation with react-bootstrap
- Lodash: tree shaking
- if statement is not working while the condition is true
- Passing props to components rendered dynamically from object