create-react-app
, then now it uses the index.html
file from the public
folder, the contents of which suits us, and the index.js
file from the src
folder, in which we write code. In particular, index.js
now looks like this: import React from "react" import ReactDOM from "react-dom" function MyInfo() { return ( <div> <h1>Bob Ziroll</h1> <p>This is a paragraph about me...</p> <ul> <li>Thailand</li> <li>Japan</li> <li>Nordic Countries</li> </ul> </div> ) } ReactDOM.render( <MyInfo />, document.getElementById("root") )
MyInfo
contained in this file. As you remember, React allows you to create many components, this is one of its strengths. It is clear that to place the code of a large number of components in a single file, although technically feasible, in practice means great inconvenience. Therefore, the code of components, even small in size, is usually made up as separate files. This approach is recommended to follow when developing React-applications.create-react-app
, in the case of create-react-app
, in the same src
folder where the index.js
file is index.js
. With this approach, the file with the MyInfo
component will be named MyInfo.js
.MyInfo.js
and transfer the code of the component MyInfo
into it, removing it from index.js
.index.js
will look like this: import React from "react" import ReactDOM from "react-dom" ReactDOM.render( <MyInfo />, document.getElementById("root") )
MyInfo.js
is: function MyInfo() { return ( <div> <h1>Bob Ziroll</h1> <p>This is a paragraph about me...</p> <ul> <li>Thailand</li> <li>Japan</li> <li>Nordic Countries</li> </ul> </div> ) }
index.js
, but the structure that we have now is not working yet.index.js
we need the import React from "react"
, even taking into account the fact that we do not directly React
here. The reason for this is that without importing React
mechanisms of this library will not work, in particular, JSX. Thanks to this import command, we were able, in previous lessons, to pass the JSX code to the ReactDOM.render()
method and output the HTML markup created on its basis to the page. All this means that in the MyInfo.js
file we also need to import React. This is a common practice for component files.MyInfo
function from the MyInfo.js
file can be used in other application files. It needs to be exported. It uses the features of the standard ES6. As a result, the updated code MyInfo.js
takes the following form: import React from "react" function MyInfo() { return ( <div> <h1>Bob Ziroll</h1> <p>This is a paragraph about me...</p> <ul> <li>Thailand</li> <li>Japan</li> <li>Nordic Countries</li> </ul> </div> ) } export default MyInfo
index.js
file. Namely, we need the MyInfo
component to be available in this file. You can make it available in index.js
importing it.react-dom
import react
in index.js
? For example, add the following import command to the file: import MyInfo from "MyInfo.js" //
create-react-app
; these dependencies can then be imported into React projects in the same way as the React library was imported). It will not find such a module, as a result the import command will not work. Therefore, the file import command must be rewritten with the path to it. In this case, we are satisfied with the indication of the current directory ( ./
) and the import command will take the following form: import MyInfo from "./MyInfo.js" //
import
command, it is important to bear in mind that it implies that JavaScript files are imported with its help. That is, the .js
extension is quite possible to remove, and the import
command, acquiring the form shown below, will not lose its operability. import MyInfo from "./MyInfo" //
index.js
file. import React from "react" import ReactDOM from "react-dom" import MyInfo from "./MyInfo" ReactDOM.render( <MyInfo />, document.getElementById("root") )
src
folder, create the components
folder for storing the files with the component code.MyInfo.js
. After that, you will need to edit the command to import this file into index.js
.MyInfo.js
indicates that this file is located in the same place as index.js
, but in fact this file is now located in the components
folder located in the same folder as index.js
. As a result, the relative path to it in index.js
will look like this: ./components/MyInfo
. Here is the updated index.js
code: import React from "react" import ReactDOM from "react-dom" import MyInfo from "./components/MyInfo" ReactDOM.render( <MyInfo />, document.getElementById("root") )
components
folder, intended for placing the code of all components, is an example of an extremely simplified project structure. In real projects, to provide the convenience of working with a large number of entities, much more complex folder structures are used. What exactly these structures will be depends on the needs of the project and on the personal preferences of the programmer.MyInfo.js
to a folder and see what comes of it, you can try to rename it, change some code in it. When in the course of such experiments, the correct work of the project will be disrupted - it will be useful to understand the problem and bring the project back to working condition.Source: https://habr.com/ru/post/433404/
All Articles