Challenge with Transferring Data to be Displayed

My issue lies in calling a function with 3 inputs - 2 numbers and 1 string. The two numbers are being passed correctly, but I need the string to be printed within the element.

I suspect the problem is related to passing the parameter to an HTML part of the code, but I am unsure how to solve it.

import Typography from '@material-ui/core/Typography';    


export function myfunction(name, min max){
    const midpoint = Math.ceil((min + max)/2)
    return(
    <div>
        <Typography id="input-slider">
            name //this is where I want name to be
        </Typography>
    <div/>
    )
}

In another file, this function is called as follows:

function main(){
    return(
        <div>
            {myfunction(MYNAME, 0, 10)}
        <div/>
    )
}

Answer №1

When creating a function myFunction as a react component, you are essentially defining a function that takes a properties object as the first argument and returns JSX.

An important point to note is that your myFunction should only accept three arguments to be considered a proper react component.

Here is an example of how a correct react component could be structured:

function MyAwesomeComponent({ name, min, max }) {
  const midpoint = Math.ceil((min + max) / 2);
  // Use curly braces around variables like {midpoint} to output their values
  return <Typography id='input-slider'>{name}</Typography>;
}

If you want to use this component within another react component, such as in the case of the main function, it's important to define it as a separate react component too.

function Main() {
  return <MyAwesomeComponent name={'YourName'} min={0} max={10} />;
}

I hope this clarifies any doubts you may have had. To dive deeper into the topic of writing react components, I highly recommend exploring the official react documentation for more insights.

Answer №2

For passing the parameter within the HTML tags, it is recommended to use a template literal. Your code should look like this:

export function myfunction(name, min max){
    const midpoint = Math.ceil((min + max)/2)
    return(
    <div>
        <Typography id="input-slider">
            ${name}
        </Typography>
    <div/>
    )
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Solving Promises with Arrays in JavaScript

Currently, I am working on a project and facing an issue that I need help with. Let me give you some background on what I am trying to achieve. First, I am making concurrent API calls using Axios in the following manner: const [...result] = await Promise. ...

Showing a notification on the screen upon redirection to the index page

On my main index page, there are 18 divs representing different books. When a user clicks on a div, they can see details about the book such as title, author, and summary. There's also an option to add the book to a Collections array by clicking the " ...

An error was encountered: An identifier that was not expected was found within the AJAX call back function

I am experiencing an issue while attempting to query an API. An Uncaught SyntaxError: Unexpected identifier is being thrown on the success part of my JQuery Ajax function. $(document).ready(function(){ $('#submitYear').click(function(){ let year ...

Having trouble with jQuery div height expansion not functioning properly?

Having a bit of trouble with my jQuery. Trying to make a div element expand in height but can't seem to get it right. Here's the script I'm using: <script> $('.button').click(function(){ $('#footer_container').anim ...

Angular routing template failing to execute inline JavaScript code

I'm facing an issue with my Angular.js routing implementation. I have successfully displayed the HTML code in templates using angular.js, but now I am encountering a problem with my template structure: <div id="map_canvas" style="width:95%; heigh ...

Record every function within the array prototype

Exploring methods that can be accessed on an array object: > console.log(Array.prototype) [] undefined > console.log(Array.prototype.push) [Function: push] Is there a way to view or log all properties/methods accessible on an object's prototyp ...

Enliven the character limit reaction by incorporating a thrilling shake animation when it reaches

I have implemented a feature in my component where if a user reaches the character limit, the component should shake. However, despite my efforts, the shaking effect is not working in the current code. const useStyles = makeStyles(() => ({ shake ...

Executing program through Socket.io alert

My NodeJS server sends notifications to clients when certain actions are performed, such as deleting a row from a grid. Socket.io broadcasts this information to all connected clients. In the example of deleting a row, one approach could be adding an `acti ...

Implementing Real-Time Search Feature Using AJAX

Exploring the world of search functions for the first time, I decided to implement an AJAX function to call a PHP file on key up. However, I encountered some strange behavior as the content in the display area was changing, but not to the expected content. ...

Unexpected behavior with VueJS Select2 directive not triggering @change event

Recently, I implemented the Select2 directive for VueJS 1.0.15 by following the example provided on their official page. However, I am facing an issue where I am unable to capture the @change event. Here is the HTML code snippet: <select v-select="ite ...

UI changes are not appearing until after the synchronous ajax call

I have two JavaScript functions that are called one after the other, as shown below. updateUI(event); syncCall(); function updateUI(event) { formSubmitBtn = $(event.target).find('[type=submit]:not(".disabled")'); formSubmitBtn.attr('di ...

PhpStorm alerts users to potential issues with Object methods within Vue components when TypeScript is being utilized

When building Vue components with TypeScript (using the lang="ts" attribute in the script tag), there is a warning in PhpStorm (version 2021.2.2) that flags any methods from the native JavaScript Object as "Unresolved function or method". For exa ...

What is the method for triggering the output of a function's value with specified parameters by clicking in an HTML

I am struggling to display a random number output below a button when it is clicked using a function. <!DOCTYPE html> <html> <body> <form> <input type="button" value="Click me" onclick="genRand()"> </form> <scri ...

Jquery's ajax function is failing to execute the server side function

I have a specific structure for my solution: My goal is to execute the recommendationProcess function from CTL_RateRecommendationDetails.ascx.cs in CTL_RateRecommendationDetails.ascx Therefore, I wrote the following code: $.ajax({ type: "POST", ...

The data structure '{ variableName: string; }' cannot be directly assigned to a variable of type 'string'

When I see this error, it seems to make perfect sense based on what I am reading. However, the reason why I am getting it is still unclear to me. In the following example, myOtherVariable is a string and variableName should be too... Or at least that&apos ...

Mongoose/JS - Bypassing all then blocks and breaking out of the code

If I need to check if a certain ID exists and exit the process if an error is encountered right from the beginning, is there a more concise way to do it rather than using an if-else block? For example: Question.find({_id: req.headers['questionid&ap ...

Navigating through div elements using arrow keys in Vue

Trying to navigate through div elements using arrow keys is proving to be a challenge for me. I have successfully achieved it in JavaScript, but I am facing difficulties doing it the "vue way". Although there should be no differences, it simply does not wo ...

Exploring the foundational element within a JSON structure

I'm trying to retrieve the album names of various artists from a JSON file located at this link. My current approach involves writing the following code: var json = JSON.parse(request.responseText); //parse the string as JSON var str = JSON.stringify ...

What are the steps to customizing a package on atmospherejs.com within meteor.js?

When working with atmosphere in meteor.js, installing a package is typically as simple as using a single command. However, if there is a need to make changes to a specific package for customization purposes, the process becomes a bit more complex. For ex ...

retrieve the data-task-IDs from the rows within the table

I am currently working with a table that looks like this: <table id="tblTasks"> <thead> <tr> <th>Name</th> <th>Due</th> ...